Re: function hoisting like var
Yuh-Ruey Chen wrote: FWIW, I definitely like this proposal more than any convoluted var-scoping block proposal. I just don't see the value of adding var-scoping blocks for the added parser complexity, and problematic return/break/continue. Unfortunately it seems people dislike the notation too much. But the complexity that you discuss must be supported anyway. Regardless whether it's written {let} or {{var}}, they both say the same thing, and what they say is supported in ES4. My code looks convoluted only because it's an odd trick. Not only does it add {let} functionality to ES3.1, it also strives very hard to make the tiniest possible change. That tiny code is also conclusive proof that {{var}} is extremely simple and has no capture problems, despite many vague claims to the contrary. Unfortunately it seems people dislike the notation so much, they prefer having no {let} functionality at all in ES3.1. -- Ingvar von Schoultz ___ Es4-discuss mailing list Es4-discuss@mozilla.org https://mail.mozilla.org/listinfo/es4-discuss
Re: function hoisting like var
It's not that your code is convoluted (although it is), it's that the added complexity to parse {{...}}, backwards incompatibility of that syntax (code generators), and the questionable aesthetics (it really is subjective) just make it infeasible. All just to provide a hoisted-let-like functionality. Plus, it's a dead horse that's been beaten one too many times :) I would like ES3.x to support let functionality, but I'd rather ES3.x just implement let statements/declarations, rather than {{...}}. I think others would agree with me here. I also prefer Igor's proposal more than {{...}}. And even if you solve problematic continuations (return/continue/break), I would like them to be generalized into first-class blocks. -Yuh-Ruey Chen On Wed, Aug 6, 2008 at 3:30 PM, Ingvar von Schoultz [EMAIL PROTECTED]wrote: Yuh-Ruey Chen wrote: FWIW, I definitely like this proposal more than any convoluted var-scoping block proposal. I just don't see the value of adding var-scoping blocks for the added parser complexity, and problematic return/break/continue. Unfortunately it seems people dislike the notation too much. But the complexity that you discuss must be supported anyway. Regardless whether it's written {let} or {{var}}, they both say the same thing, and what they say is supported in ES4. My code looks convoluted only because it's an odd trick. Not only does it add {let} functionality to ES3.1, it also strives very hard to make the tiniest possible change. That tiny code is also conclusive proof that {{var}} is extremely simple and has no capture problems, despite many vague claims to the contrary. Unfortunately it seems people dislike the notation so much, they prefer having no {let} functionality at all in ES3.1. -- Ingvar von Schoultz ___ Es4-discuss mailing list Es4-discuss@mozilla.org https://mail.mozilla.org/listinfo/es4-discuss
Re: function hoisting like var
Okay then, even shorter. Brendan Eich wrote: But it is not what you proposed. In what way? Please be more specific, because I don't know what this supposed proposal of mine is. For example, those scope questions mention rebinding in a visible scope, midway through its code block, but that's unrelated to anything I've proposed and unrelated to how JavaScript works. I've tried to guess what you think I've proposed from the scope questions, but the guessing has made my answers long. Stupid mistake, I should have asked instead, sorry. So please say something specific that I can address. Waldemar wrote a while back: Keep in mind that function assignments hoist to the beginning of the scope in which the function is defined, so your proposal won't work. It works perfectly well. The initial |undefined| is fully intentional, it protects against capture problems. It's a minor limitation, certainly not a showstopper. -- Ingvar von Schoultz ___ Es4-discuss mailing list Es4-discuss@mozilla.org https://mail.mozilla.org/listinfo/es4-discuss
Re: function hoisting like var
mode that required me to use an |outer| declaration, as a much clearer warning that I'm accessing something external: var a = 10, b = 20; function f() { outer a; c = outer b; } This would be /much/ more useful than data types in very small programs. And so on. The above is the /exact/ functionality of function hoisting like var, apart from using two names. You can refuse the clearer syntax, but you can't refuse the above code and functionality. I think I see the confusion now. Do you believe that in the var b = a; code you wrote, both the binding of the var named b *and* its initialization with the value of the function object denoted a are hoisted? Hoisted up to what point? No, it starts out |undefined|, and that's a good thing, as detailed above. The assignment stays in place and occurs where it's written in the code. I couldn't express this with exactness in that code snippet. In my snippet's inner scope the function is assigned (is callable) from before you enter the inner scope, since it's written as a declaration rather than an assignment. Once again, this is exactly as should be. That snippet is carefully crafted. The main difference between a real hoisted function and my snippet is that my snippet shows the function with two different names, one illustrating the inner-scope assignment and the other illustrating the outer-scope assignment, whereas a real hoisted function would have only one name, bound only in the outer scope. I could instead have written my snippet like this instead: { var a = function a(){} } Then it's only one name, hoisting out and bound in the outer scope, so in that regard, this is more similar to a real hoisting function. However, this does not reflect the fact that the function should be assigned (should be callable) from before you enter the inner scope. Since I wanted to refute the claim about complexity, I found it more relevant to show correct hoisting and assignment than solving the triviality of having two names. In fact showing it with two names might even help a little. It may help make clear and specific how there are two scopes involved, and how each one is dealt with by re-using existing functionality. (Or rather, I get the impression that you can re-use existing functionality, largely, and I've seen no specific counterargument so far.) So the one and only outer name of the hoisted function gets assigned the callable function just before we enter the inner scope. Waldemar wrote a while back: Keep in mind that function assignments hoist to the beginning of the scope in which the function is defined, so your proposal won't work. The word assignment where definition was perhaps more precise (function definitions replace extant properties of the same name in the variable object, they are not equivalent to assignment expressions) may have misled you. From the context and the long- standing spec and implementation behavior with functions not in blocks or any other sub-statement position, it was clear (I think) what was meant, but I can see how this could be confusing. Assignment expressions and initializers in var statements do not hoist or otherwise move in the flow of control. I hope I've shown now that I understand all this quite clearly, and intended exactly this behavior, and consider it necessary, acceptable, and very useful. I greatly appreciate your reply and questions. Detailed and specific, clarifying to me what I need to explain and answer. Perfect for my somewhat overly detailed mind. Thank you very much. I hope my long answer hasn't been too exhausting to read. -- Ingvar von Schoultz --- (My quirky use of capitals in code comes from my opinion that reserved and predefined words should all start with lowercase, and user-defined should all start with uppercase, because this will easily and elegantly prevent a host of name-collision problems when things like programming languages are upgraded with new labels.) ___ Es4-discuss mailing list Es4-discuss@mozilla.org https://mail.mozilla.org/listinfo/es4-discuss
Re: A simple translation of the scoping-block syntax sugar -- Was: Re: function hoisting like var
Ingvar von Schoultz wrote: Any variable that you declare anywhere will simply splash out of any { } and attach itself to the nearest surrounding {{ }}, becoming visible in the entire space between them. Int this splashing out of { }, function declarations are a special case. Although implementations differ, I think {{ }} could serve as an opt-in where they become specified to follow clear rules. In my opinion the most useful rules would be: The name is visible in the entire space between the nearest surrounding {{ }}. Usually it's also callable in that entire space, but there are a few exceptions. If you put the declaration in a sequential construct, for example under if(), then at the {{ you can't call it (it's |undefined|), and it becomes callable only from the sequential construct and onward. If there is a { at the beginning of the sequential construct, the function becomes callable from the { and onward. If you make more than one declaration with the same name, this makes all the declarations of that name sequential. Then at {{ you can't call any of the versions of the function (again |undefined|). Each version becomes callable at the spot of declaration. In all other cases the function is callable in the entire space between {{ }}. (This is not intended as an exhaustively detailed description, just an overview.) (The rules will have to be changed a little if let declarations are allowed between {{ }}, or if allowing them there in a future version should be possible. And reserving the possibility might be a good idea -- who knows what people will want or will invent. To allow that, the rules become slightly more limiting.) -- Ingvar von Schoultz --- (My quirky use of capitals in code comes from my opinion that reserved and predefined words should all start with lowercase, and user-defined should all start with uppercase, because this will easily and elegantly prevent a host of name-collision problems when things like programming languages are upgraded with new labels.) ___ Es4-discuss mailing list Es4-discuss@mozilla.org https://mail.mozilla.org/listinfo/es4-discuss
Re: function hoisting like var
On Jul 30, 2008, at 3:13 PM, Ingvar von Schoultz wrote: Regarding my explanations quoted below, did they clarify things? No, and I don't have time right now to deal with the great number of words you have dedicated to promoting your ideas. This is a shame, since you could have a point, but I simply can't expend the effort to try to find it given other priorities. Sorry, this is not something I'm happy about, but it's not entirely due to my being busy (i.e., it's not just me -- it's you :-/). May I suggest using fewer words when replying than you have? Generally proportionate responses would be best. I'm assuming fair play, i.e., that people are not resorting to too short and dismissive a style of replying. I'm not doing that here, I'm just letting you know I don't have time to plow through what you have written. Maybe someone else on the list has the time. /be ___ Es4-discuss mailing list Es4-discuss@mozilla.org https://mail.mozilla.org/listinfo/es4-discuss
A simple translation of the scoping-block syntax sugar -- Was: Re: function hoisting like var
The simple translation of {{ }} that I posted yesterday needs an improvement. It must pass |this| and |arguments| to the scoping function. I still think it looks quite small and simple, and that it would be a very useful addition to the language. try { var _ReturnValue = (function (arguments) { // Any governing for(), while() or switch() goes here. { // The code between {{ }} goes here. } throw _NormalEndSignal; }).call (this, arguments); return _ReturnValue; } catch (_Sig) { if (_Sig != _NormalEndSignal) throw _Sig; } We must use .call(), not .apply(), so that we pass the arguments object as such, with the outer function as arguments.callee. In the global scope, |arguments| will be undefined, unless the programmer has defined it. It should remain undefined inside the scoping function. As an alternative to the above parameter passing, I suppose the implementation can simply remove the scope function's arguments object, thereby making the outer arguments object visible. Initial underscore means hidden, invisible. For a description of the translation, see the quoted text below. Ingvar Ingvar von Schoultz wrote: Ingvar von Schoultz wrote: In theory {{ code }} could be converted to a function that returns information about whatever break/continue/return was reached. I now think this could be made very simple. The solution is to make this version slightly limited. Don't support break/continue statements that mention any label outside {{ }}. Leave that for a future version. This simplifies things tremendously! And you don't need that kind of break/continue all that often. (But a future version must include it.) This limited solution only needs break/continue functionality that is already supported. Even the necessary label checking is already supported. An unsupported break such as the following gives a syntax error if you translate {{ }} to a one-shot function: Outer: for (var OutName in OutThing) for (var InName in InThing) {{ break Outer; }} The error message that I get says undefined label, which is misleading, but apart from that it works perfectly. If people find the above limited support for break/continue acceptable for this version, all that remains is the return statement. It seems easiest to use a solution that leaves all the return statements in the original code intact. This makes the translation from {{ }} to function a little elaborate and odd, but everything is nicely contained at one spot. Within the created function, if control reaches the final }}, throw a special error that isn't really an error, rather the opposite, it's a signal that indicates that this is a normal termination. When this signal is received, continue below }}. If instead, within the created function, control reaches a return statement that appears in the original code, the result is a regular return, without the above thrown signal. This indicates that whatever value was returned should be passed along as return value of the enclosing function. This means that {{ }} is essentially translated into this: try { var ReturnValue = (function() { // The code that was written between {{ }} goes here. throw NormalEndSignal; })(); return ReturnValue; } catch (Sig) { if (Sig != NormalEndSignal) throw Sig; } It seems to me that this becomes very nicely contained, very manageable. Implementations will probably want to optimize away this entire elaborate arrangement and replace it with plain and simple scoping blocks. But they can take their time. The above provides the functionality in a simple way. That is, unless I've missed something, and the above solution doesn't work the way I think it does. -- Ingvar von Schoultz --- (My quirky use of capitals in code comes from my opinion that reserved and predefined words should all start with lowercase, and user-defined should all start with uppercase, because this will easily and elegantly prevent a host of name-collision problems when things like programming languages are upgraded with new labels.) ___ Es4-discuss mailing list Es4-discuss@mozilla.org https://mail.mozilla.org/listinfo/es4-discuss
Re: A simple translation of the scoping-block syntax sugar -- Was: Re: function hoisting like var
If the simple translation of {{ }} is used, any governing for(), while() or switch() must be moved inside the scoping function. A very simple, very minimalistic approach may be enough. for (var Name in Thing) {{ }} Name is locally contained inside {{ }} while Thing is outside. But maybe the compiler doesn't have to make this distinction. Personally I'd never use the same name with different meanings so close together, like this: var Thing = ...; for (var Name in Thing) {{ var Thing = ...; }} I find that obscure and error-prone. If the limitation is acceptable, a very minimalistic translation of {{ }} could simply move the entire for (...) inside the scoping function, and leave it at that. As long as Thing isn't redeclared inside, the outer Thing is visible and it works. Does this make it simple enough for ES3.1? As an added bonus, the functionality becomes very easy to explain. The rules become very plain and simple. I don't know if a let statement with this limitation would be useful. Every description of let that I can find re-uses names. But if it's useful, it could be trivially implemented as syntax sugar: let (a = x, b = y) {{ }} would be syntax sugar for {{ var a = x, b = y; }} However let has different semantics in at least one existing implementation (it does distinguish between same-name variables in the two scopes), so if a minimalistic let is introduced, it should probably use a different keyword. -- Ingvar von Schoultz --- (My quirky use of capitals in code comes from my opinion that reserved and predefined words should all start with lowercase, and user-defined should all start with uppercase, because this will easily and elegantly prevent a host of name-collision problems when things like programming languages are upgraded with new labels.) ___ Es4-discuss mailing list Es4-discuss@mozilla.org https://mail.mozilla.org/listinfo/es4-discuss
Re: A simple translation of the scoping-block syntax sugar -- Was: Re: function hoisting like var
I'd like to summarize some clarifications and arguments. I'm thinking of what it might be like if {{ }} should become available in ES3.1. === Intuitive === The proposed {{ }} mean exactly the same thing as { } in C, Java etc. If switching between languages causes you any difficulties, simply use {{ }} everywhere in your ES3.1 code. This way you get the braces that you're used to from C and Java, apart from a slightly different notation. Think of it as JavaScript having a small difference in brace notation while the functionality is the same. === Control flow === In ES3 you can use { } for plain control flow, without any scoping. This is very useful for small projects. If this is not a good fit for your projects, it's still very useful elsewhere. Please don't deprecate this functionality. For more on the usefulness of this plain control flow, see: https://mail.mozilla.org/pipermail/es4-discuss/2008-July/003353.html === Difference === If you want the full flexibility of using both {{ }} and { }, first decide which type of brace you want to use most in your program. Stick with that almost everywhere. Make an exception only when you have a clear and specific reason, because you need the functionality of the other type. This way your code will be consistent and readable. You can view the {{ }} as containers for variables. Any variable that you declare anywhere will simply splash out of any { } and attach itself to the nearest surrounding {{ }}, becoming visible in the entire space between them. If you prefer a more low-level view, { } are plain jumps and branches, and only {{ }} affect the scope chain. === Ugly === The proposed {{ }} will inevitably look horrible in emails if you use a proportional font such as Times New Roman. Real code is never displayed in such a font. Try a programmer's editor with a suitable font and syntax coloring. It looks completely different! If people still find it ugly, I hope a good alternative can be found, because the functionality would be very useful. === Let declarations === There are existing implementations that support let. My proposal is that /let declarations/ should be completely disabled between {{ }}. This of course includes all { } that are nested between {{ }}. The only reason is simplicity, mainly for the script programmer. Outside {{ }} they should be enabled, for compatibility and to satisfy those who prefer the let and var combination. -- Ingvar von Schoultz --- (My quirky use of capitals in code comes from my opinion that reserved and predefined words should all start with lowercase, and user-defined should all start with uppercase, because this will easily and elegantly prevent a host of name-collision problems when things like programming languages are upgraded with new labels.) ___ Es4-discuss mailing list Es4-discuss@mozilla.org https://mail.mozilla.org/listinfo/es4-discuss
Re: A simple translation of the scoping-block syntax sugar -- Was: Re: function hoisting like var
The translation can be made simpler. First, define a unique value, to be returned when the end of the scoping block is reached. If done in JavaScript: var _EndOfBlock = {}; Then each {{ }} can be translated to this: var _ReturnValue = (function (arguments) { // Any governing for(), while() or switch() goes here. { // The code between {{ }} goes here. } return _EndOfBlock; }).call (this, arguments); if (_ReturnValue != _EndOfBlock) return _ReturnValue; Ingvar Ingvar von Schoultz wrote: The simple translation of {{ }} that I posted yesterday needs an improvement. It must pass |this| and |arguments| to the scoping function. I still think it looks quite small and simple, and that it would be a very useful addition to the language. try { var _ReturnValue = (function (arguments) { // Any governing for(), while() or switch() goes here. { // The code between {{ }} goes here. } throw _NormalEndSignal; }).call (this, arguments); return _ReturnValue; } catch (_Sig) { if (_Sig != _NormalEndSignal) throw _Sig; } We must use .call(), not .apply(), so that we pass the arguments object as such, with the outer function as arguments.callee. In the global scope, |arguments| will be undefined, unless the programmer has defined it. It should remain undefined inside the scoping function. As an alternative to the above parameter passing, I suppose the implementation can simply remove the scope function's arguments object, thereby making the outer arguments object visible. Initial underscore means hidden, invisible. For a description of the translation, see the quoted text below. Ingvar Ingvar von Schoultz wrote: Ingvar von Schoultz wrote: In theory {{ code }} could be converted to a function that returns information about whatever break/continue/return was reached. I now think this could be made very simple. The solution is to make this version slightly limited. Don't support break/continue statements that mention any label outside {{ }}. Leave that for a future version. This simplifies things tremendously! And you don't need that kind of break/continue all that often. (But a future version must include it.) This limited solution only needs break/continue functionality that is already supported. Even the necessary label checking is already supported. An unsupported break such as the following gives a syntax error if you translate {{ }} to a one-shot function: Outer: for (var OutName in OutThing) for (var InName in InThing) {{ break Outer; }} The error message that I get says undefined label, which is misleading, but apart from that it works perfectly. If people find the above limited support for break/continue acceptable for this version, all that remains is the return statement. It seems easiest to use a solution that leaves all the return statements in the original code intact. This makes the translation from {{ }} to function a little elaborate and odd, but everything is nicely contained at one spot. Within the created function, if control reaches the final }}, throw a special error that isn't really an error, rather the opposite, it's a signal that indicates that this is a normal termination. When this signal is received, continue below }}. If instead, within the created function, control reaches a return statement that appears in the original code, the result is a regular return, without the above thrown signal. This indicates that whatever value was returned should be passed along as return value of the enclosing function. This means that {{ }} is essentially translated into this: try { var ReturnValue = (function() { // The code that was written between {{ }} goes here. throw NormalEndSignal; })(); return ReturnValue; } catch (Sig) { if (Sig != NormalEndSignal) throw Sig; } It seems to me that this becomes very nicely contained, very manageable. Implementations will probably want to optimize away this entire elaborate arrangement and replace it with plain and simple scoping blocks. But they can take their time. The above provides the functionality in a simple way. That is, unless I've missed something, and the above solution doesn't work the way I think it does. -- Ingvar von Schoultz --- (My quirky use of capitals in code comes from my opinion that reserved and predefined words should all start with lowercase, and user-defined should all start with uppercase, because this will easily and elegantly prevent a host of name-collision problems when things like programming languages are upgraded with new labels.)
Re: function hoisting like var
Ingvar von Schoultz wrote: In theory {{ code }} could be converted to a function that returns information about whatever break/continue/return was reached. But I'd be quite surprised if that is easy. Something like this. Written with syntax sugar: Outer: for (var OutName in OutThing) for (var InName in InThing) {{ break Outer; }} Translation: Outer: for (var OutName in OutThing) { var _Result = (function (_InThing) { for (var InName in _InThing) { return ({JumpSpot: 'break Outer'}) } })(InThing); if (_Result.JumpSpot == 'break Outer') break Outer; } The inner for() is part of the scoping block, so it belongs inside, even though the original code has it above. We must make sure the value of InThing is available inside even if the name is declared for a different variable inside. I use initial underscore to indicate something internal and invisible. It doesn't look complicated here! Unfortunately these things have a terrible tendency to grow in complexity... I now think this could be made very simple. The solution is to make this version slightly limited. Don't support break/continue statements that mention any label outside {{ }}. Leave that for a future version. This simplifies things tremendously! And you don't need that kind of break/continue all that often. (But a future version must include it.) This limited solution only needs break/continue functionality that is already supported. Even the necessary label checking is already supported. An unsupported break such as the following gives a syntax error if you translate {{ }} to a one-shot function: Outer: for (var OutName in OutThing) for (var InName in InThing) {{ break Outer; }} The error message that I get says undefined label, which is misleading, but apart from that it works perfectly. If people find the above limited support for break/continue acceptable for this version, all that remains is the return statement. It seems easiest to use a solution that leaves all the return statements in the original code intact. This makes the translation from {{ }} to function a little elaborate and odd, but everything is nicely contained at one spot. Within the created function, if control reaches the final }}, throw a special error that isn't really an error, rather the opposite, it's a signal that indicates that this is a normal termination. When this signal is received, continue below }}. If instead, within the created function, control reaches a return statement that appears in the original code, the result is a regular return, without the above thrown signal. This indicates that whatever value was returned should be passed along as return value of the enclosing function. This means that {{ }} is essentially translated into this: try { var ReturnValue = (function() { // The code that was written between {{ }} goes here. throw NormalEndSignal; })(); return ReturnValue; } catch (Sig) { if (Sig != NormalEndSignal) throw Sig; } It seems to me that this becomes very nicely contained, very manageable. Implementations will probably want to optimize away this entire elaborate arrangement and replace it with plain and simple scoping blocks. But they can take their time. The above provides the functionality in a simple way. That is, unless I've missed something, and the above solution doesn't work the way I think it does. -- Ingvar von Schoultz --- (My quirky use of capitals in code comes from my opinion that reserved and predefined words should all start with lowercase, and user-defined should all start with uppercase, because this will easily and elegantly prevent a host of name-collision problems when things like programming languages are upgraded with new labels.) ___ Es4-discuss mailing list Es4-discuss@mozilla.org https://mail.mozilla.org/listinfo/es4-discuss
Re: function hoisting like var
Brendan Eich wrote: On Jul 26, 2008, at 2:06 PM, Ingvar von Schoultz wrote: How sad! It seemed such a simple and intuitive notation! Opinions vary, but all the ones I heard at the Ecma TC39 meeting found it neither simple nor intuitive, and some abhorred it on aesthetic grounds to boot. Not simple? How is that possible? There have been several misunderstandings. Did they spread that far? {{ }} is just the same as a scoping function used on ES3: (function() { code })() Everything works just the way I meant if you take ES3, or any platform that does /not/ support let declarations, and make it so that {{ }} becomes syntax sugar for the above one-shot scoping function. Of course, the way I think about {{ }}, I don't see it as a function, I see it as a name-binding scope. But the scoping effect is just that. For me, having a single scoping block is simpler than having two, and having a single visibility keyword is simpler than having two. And I think my notation is /much/ simpler than the one-shot function! {{ code }} (function() { code })() I do find my notation slightly ugly. But the one-shot function is worse in my view. Charming but very kludgey. (Thanks Igor for noticing that {{ }} are synonymous with one-shot scoping functions. Why didn't I think of that!) I think all of these would be unambiguous: {. code .} {: code :} {| code |} {[ code ]} [[ code ]] [ code ] These are either syntax errors without opt-in versioning, Yes, and this should guarantee that they are unique. I now have the impression that, on the client side, opt-in by version would be necessary in any case, with any notation. If old platforms accept the syntax, it changes existing semantics; if they don't, it requires opt-in to avoid errors. or (the last three) do create incompatible ambiguity (consider array initialisers). It seems to me that they could be disambiguated easily. I comment on this in a reply to Igor. What's more, as Waldemar pointed out many threads (and too many words) ago, they create capture problems. There were misunderstandings. If there are fundamental differences in this regard between {{ }} and one-shot scoping functions, I can't find them. Please work through the last mail I sent before replying; if some vocabulary or infelicitous word choice is causing any confusion, feel free to mail me privately and ask pointed questions. Thanks, Thanks for the invitation. I'll certainly take you up on it if the need or urge arises. -- Ingvar von Schoultz --- (My quirky use of capitals in code comes from my opinion that reserved and predefined words should all start with lowercase, and user-defined should all start with uppercase, because this will easily and elegantly prevent a host of name-collision problems when things like programming languages are upgraded with new labels.) ___ Es4-discuss mailing list Es4-discuss@mozilla.org https://mail.mozilla.org/listinfo/es4-discuss
Re: function hoisting like var
2008/7/28 Ingvar von Schoultz [EMAIL PROTECTED]: I wonder if people would like using them. Anything that is available in a browser will be used and abused ;). {[ code ]} [[ code ]] [ code ] Any of this can be a valid ES3 code as [] always means an array literal. I get the impression that disambiguation would be easy. eval('[]') is a valid and, in fact, useful ES3 code. Similarly eval('{[]}'). Regards, Igor ___ Es4-discuss mailing list Es4-discuss@mozilla.org https://mail.mozilla.org/listinfo/es4-discuss
Re: function hoisting like var
2008/7/28 Ingvar von Schoultz [EMAIL PROTECTED]: {{ }} is just the same as a scoping function used on ES3: (function() { code })() As Lars Hansen has pointed out any proposal for a shorthand for (function() { code })() has to deal with break/continue/return inside the code. This is the the reason if anything I would prefer in ES3.1 just shorthands for function definitions like function() expr - equivalent to function () { return expr; }. This is already in ES4 and is implemented by at least on implementation (SpiderMonkey). or function optional_name { code } - equivalent to function optional_name() { code } - a hypothetical shortcut that would allow to write a pseudo-blocks like function { }(); with clear emphasis that this is a lambda with usual rules for break/continue/return. Regards, Igor ___ Es4-discuss mailing list Es4-discuss@mozilla.org https://mail.mozilla.org/listinfo/es4-discuss
Re: function hoisting like var
Mike Shaver wrote: On Sun, Jul 27, 2008 at 7:51 PM, Ingvar von Schoultz [EMAIL PROTECTED] wrote: Igor Bukanov wrote: eval('[]') is a valid and, in fact, useful ES3 code. Similarly eval('{[]}'). Yes, and by my rules they both create and return a new, empty array, which is intuitively expected and compatible. And the latter only does because the { is parsed as starting a block; if it were to start out assuming a literal, you would get an illegal object initializer. I meant that [ would follow those rules, not {. The rules I sketched are definitely not usable for {. I get the impression that the parser always must start out assuming it's a literal, and backtrack if the syntax doesn't fit. The only exception would be at a spot where a literal isn't possible while a block is possible, but I don't think any such spot is possible. if { block; } would seem to be such a spot, no? These are valid literals (immediately discarded, but valid syntax): if (true) [10, 20]; if (true) { [10, 20] }; Likewise valid: if (true) 10; Regardless, I'm pretty sure ES3 compat requires that a statement starting with a { be taken as a block and not a literal, with no backtracking. js {a:5, b:6} typein:4: SyntaxError: invalid label: typein:4: {a:5, b:6} typein:4: ..^ Mike ___ Es4-discuss mailing list Es4-discuss@mozilla.org https://mail.mozilla.org/listinfo/es4-discuss -- Ingvar von Schoultz --- (My quirky use of capitals in code comes from my opinion that reserved and predefined words should all start with lowercase, and user-defined should all start with uppercase, because this will easily and elegantly prevent a host of name-collision problems when things like programming languages are upgraded with new labels.) ___ Es4-discuss mailing list Es4-discuss@mozilla.org https://mail.mozilla.org/listinfo/es4-discuss
Re: function hoisting like var
Ingvar von Schoultz wrote: Igor Bukanov wrote: 2008/7/28 Ingvar von Schoultz wrote: snip Any of this can be a valid ES3 code as [] always means an array literal. I get the impression that disambiguation would be easy. eval('[]') is a valid and, in fact, useful ES3 code. Similarly eval('{[]}'). Yes, and by my rules they both create and return a new, empty array, which is intuitively expected and compatible. Are you saying that a block then may never be empty as a syntax rule? ES 3 blocks are allowed to be empty and I bet there are examples where blocks contain nothing but an IE/JScript conditional comment and so are going to be interpreted as being empty by other ES 3 implementations. (Comments are allowed inside array literal definitions at present). When I said that an array is a comma-separated list of values, the fact that I left out the empty and the single-value cases didn't mean that they should be seen as blocks! I just didn't want to write a lengthy, exhaustive full grammar... snip { 1,2,3,4,5 } - is a valid ES 3 Program (even if a pointless one); A Block statement containing an Expression statement (with automatic semi-colon insertion making the Expression statement into - 1,2,3,4,5; -). eval('{1,2,3,4,5}') - results in the value 5, while - eval('[1,2,3,4,5]') - returns a 5 element array. ___ Es4-discuss mailing list Es4-discuss@mozilla.org https://mail.mozilla.org/listinfo/es4-discuss
Re: function hoisting like var
Igor Bukanov wrote: 2008/7/28 Ingvar von Schoultz [EMAIL PROTECTED]: If [...] is preceded by =, or enclosed in ( ), or in some other position where you can't have a block, it's a literal. If it's a literal it must contain a comma-separated list of values, so if the syntax doesn't match this, it's a block. Such rules require an arbitrary look ahead in the parser so it can distinguish that in cases like if (x) [arbitrary_expression;] [] means a block while in if (x) [arbitrarily_expression]; [] would mean a literal. This would require a mayor change in most if not all current ES parser implementations. There were some proposals for ES4 syntax that would require such look ahead, but they were rejected not only technical grounds but also on the grounds that such look ahead poses comprehension problem for a human brain! And here are we talking about a minimalistic sugar for ES3.1. Regards, Igor ___ Es3.x-discuss mailing list [EMAIL PROTECTED] https://mail.mozilla.org/listinfo/es3.x-discuss I wasn't aware that backtracking was a difficulty. That makes quite a difference! In theory this could be overcome. Often the ambiguity doesn't matter: if (x) [a(), b(), c()]; Block or throwaway array, the end result is the same. So in principle you could start out building an array, and on the first nonmatching syntax you discard the half-built array and build a scope block instead: if (x) [a(), b(), c(), d(), e(), let f = g;] Of course in practice such a solution is too messy, unless you have some extremely thorny problem that just can't be solved in any other way. And this certainly isn't that kind of problem. So [ ] are out. Unless they're reincarnated as: [: code :] Ah, now I know! I have the perfect solution, guaranteed to be unambiguous. Happy code! (-: Scoping blocks are delimited by smileys! :-) -- Ingvar von Schoultz --- (My quirky use of capitals in code comes from my opinion that reserved and predefined words should all start with lowercase, and user-defined should all start with uppercase, because this will easily and elegantly prevent a host of name-collision problems when things like programming languages are upgraded with new labels.) ___ Es4-discuss mailing list Es4-discuss@mozilla.org https://mail.mozilla.org/listinfo/es4-discuss
Re: function hoisting like var
Richard Cornford wrote: Ingvar von Schoultz wrote: Igor Bukanov wrote: 2008/7/28 Ingvar von Schoultz wrote: snip Any of this can be a valid ES3 code as [] always means an array literal. I get the impression that disambiguation would be easy. eval('[]') is a valid and, in fact, useful ES3 code. Similarly eval('{[]}'). Yes, and by my rules they both create and return a new, empty array, which is intuitively expected and compatible. Are you saying that a block then may never be empty as a syntax rule? ES 3 blocks are allowed to be empty and I bet there are examples where blocks contain nothing but an IE/JScript conditional comment and so are going to be interpreted as being empty by other ES 3 implementations. (Comments are allowed inside array literal definitions at present). Does it matter whether it's interpreted as an empty block or an empty discarded array? When I said that an array is a comma-separated list of values, the fact that I left out the empty and the single-value cases didn't mean that they should be seen as blocks! I just didn't want to write a lengthy, exhaustive full grammar... snip { 1,2,3,4,5 } - is a valid ES 3 Program (even if a pointless one); A Block statement containing an Expression statement (with automatic semi-colon insertion making the Expression statement into - 1,2,3,4,5; -). eval('{1,2,3,4,5}') - results in the value 5, while - eval('[1,2,3,4,5]') - returns a 5 element array. Yes. And if the rules for { remain unchanged, and [ starts out assuming it's a literal, you get the same result with my rules. -- Ingvar von Schoultz --- (My quirky use of capitals in code comes from my opinion that reserved and predefined words should all start with lowercase, and user-defined should all start with uppercase, because this will easily and elegantly prevent a host of name-collision problems when things like programming languages are upgraded with new labels.) ___ Es4-discuss mailing list Es4-discuss@mozilla.org https://mail.mozilla.org/listinfo/es4-discuss
Re: function hoisting like var
On Sun, Jul 27, 2008 at 9:15 PM, Ingvar von Schoultz [EMAIL PROTECTED] wrote: Does it matter whether it's interpreted as an empty block or an empty discarded array? It's not always discarded: js var x = 1 js eval (if (x) [1, 2, 3]) 1,2,3 Mike ___ Es4-discuss mailing list Es4-discuss@mozilla.org https://mail.mozilla.org/listinfo/es4-discuss
Re: function hoisting like var
Mike Shaver wrote: On Sun, Jul 27, 2008 at 9:15 PM, Ingvar von Schoultz [EMAIL PROTECTED] wrote: Does it matter whether it's interpreted as an empty block or an empty discarded array? It's not always discarded: js var x = 1 js eval (if (x) [1, 2, 3]) 1,2,3 That becomes an array, no problem. My Does it matter was about empty blocks only, in reply to your question: Are you saying that a block then may never be empty as a syntax rule? Does it matter whether it's interpreted as an empty block or an empty discarded array? Array is the default. An empty block gets misinterpreted as an empty array. So a code generator that produces an eval that returns a scoping block must deal with the special case that the block may be empty or contain a comma-separated list of values. It's the generator's own fault for producing such silly code, so I don't feel sorry for that generator. :-) -- Ingvar von Schoultz --- (My quirky use of capitals in code comes from my opinion that reserved and predefined words should all start with lowercase, and user-defined should all start with uppercase, because this will easily and elegantly prevent a host of name-collision problems when things like programming languages are upgraded with new labels.) ___ Es4-discuss mailing list Es4-discuss@mozilla.org https://mail.mozilla.org/listinfo/es4-discuss
Re: function hoisting like var
Ingvar von Schoultz wrote: Richard Cornford wrote: snip { 1,2,3,4,5 } - is a valid ES 3 Program (even if a pointless one); A Block statement containing an Expression statement (with automatic semi-colon insertion making the Expression statement into - 1,2,3,4,5; -). eval('{1,2,3,4,5}') - results in the value 5, while - eval('[1,2,3,4,5]') - returns a 5 element array. Yes. And if the rules for { remain unchanged, and [ starts out assuming it's a literal, you get the same result with my rules. And if I wanted that block scope this - eval({[\n eval('var a = 1'),eval('var b = 2'),eval('a + b')\n]}); - will still give me an array (and side effects in the containing scope)? ___ Es4-discuss mailing list Es4-discuss@mozilla.org https://mail.mozilla.org/listinfo/es4-discuss
Re: function hoisting like var
Richard Cornford wrote: Ingvar von Schoultz wrote: Richard Cornford wrote: snip { 1,2,3,4,5 } - is a valid ES 3 Program (even if a pointless one); A Block statement containing an Expression statement (with automatic semi-colon insertion making the Expression statement into - 1,2,3,4,5; -). eval('{1,2,3,4,5}') - results in the value 5, while - eval('[1,2,3,4,5]') - returns a 5 element array. Yes. And if the rules for { remain unchanged, and [ starts out assuming it's a literal, you get the same result with my rules. And if I wanted that block scope this - eval({[\n eval('var a = 1'),eval('var b = 2'),eval('a + b')\n]}); - will still give me an array (and side effects in the containing scope)? Goodness, if the code generator writes that, it deserves to melt! I'd say if you're aware of scoping blocks, and want to have a scoping block, and despite this insist on separating with commas rather than semicolons or bare newlines, well, then you deserve the array that you get! So the solution is to use semicolon or newline as separator, preferably somewhere near the beginning. Of course the real solution to all this is to use my original {{ }} instead. You get used to them. And they're very easy to type! -- Ingvar von Schoultz --- (My quirky use of capitals in code comes from my opinion that reserved and predefined words should all start with lowercase, and user-defined should all start with uppercase, because this will easily and elegantly prevent a host of name-collision problems when things like programming languages are upgraded with new labels.) ___ Es4-discuss mailing list Es4-discuss@mozilla.org https://mail.mozilla.org/listinfo/es4-discuss
Re: function hoisting like var
Igor Bukanov wrote: 2008/7/28 Ingvar von Schoultz [EMAIL PROTECTED]: {{ }} is just the same as a scoping function used on ES3: (function() { code })() As Lars Hansen has pointed out any proposal for a shorthand for (function() { code })() has to deal with break/continue/return inside the code. Indeed I wasn't thinking of that in my description. This is the the reason if anything I would prefer in ES3.1 just shorthands for function definitions like function() expr - equivalent to function () { return expr; }. This is already in ES4 and is implemented by at least on implementation (SpiderMonkey). I rarely feel a need for such tiny scoping blocks, but often long for big scoping blocks with intuitive syntax. function optional_name { code } - equivalent to function optional_name() { code } - a hypothetical shortcut that would allow to write a pseudo-blocks like function { }(); with clear emphasis that this is a lambda with usual rules for break/continue/return. Yes, we can't have syntax that looks like a block but forbids or misunderstands break/continue/return. In theory {{ code }} could be converted to a function that returns information about whatever break/continue/return was reached. But I'd be quite surprised if that is easy. Something like this. Written with syntax sugar: Outer: for (var OutName in OutThing) for (var InName in InThing) {{ break Outer; }} Translation: Outer: for (var OutName in OutThing) { var _Result = (function (_InThing) { for (var InName in _InThing) { return ({JumpSpot: 'break Outer'}) } })(InThing); if (_Result.JumpSpot == 'break Outer') break Outer; } The inner for() is part of the scoping block, so it belongs inside, even though the original code has it above. We must make sure the value of InThing is available inside even if the name is declared for a different variable inside. I use initial underscore to indicate something internal and invisible. It doesn't look complicated here! Unfortunately these things have a terrible tendency to grow in complexity... -- Ingvar von Schoultz --- (My quirky use of capitals in code comes from my opinion that reserved and predefined words should all start with lowercase, and user-defined should all start with uppercase, because this will easily and elegantly prevent a host of name-collision problems when things like programming languages are upgraded with new labels.) ___ Es4-discuss mailing list Es4-discuss@mozilla.org https://mail.mozilla.org/listinfo/es4-discuss
Re: function hoisting like var
On Jul 26, 2008, at 4:03 AM, Ingvar von Schoultz wrote: [EMAIL PROTECTED] wrote: Ingvar von Schoultz wrote: [EMAIL PROTECTED] wrote: I'm trying to keep the language relatively simple. You can't get away from supporting this: { function a(){} var b = a; } What do you mean? This is a syntax error in both ES3 and ES3.1. It works fine in Firefox 2, Konqueror 3, Opera 9, Internet Explorer 6, and server-side Rhino with JavaScript 1.6. Waldemar meant precisely what he wrote: ES3 and draft ES3.1 -- the specifications, not random JS implementations. Five platforms out of five. Can you throw a syntax error here and claim to be compatible? The implementations are not compatible. Please see the earlier es4- discuss thread with subject Function declarations in statements at: https://mail.mozilla.org/pipermail/es4-discuss/2007-March/ thread.html#527 It does not already exist in ES3 or ES3.1. It exists on platforms as described above. I assumed that ES4 would be compatible. No, because it is impossible to be compatible with conflicting extensions to ES3 that browsers have implemented. The conflicts and undesirable intersection semantics are why ES4 proposes, and ES3.1 considered but deferred, block-scoped functions that must be direct children of braced blocks. This requires opt-in versioning, which is why ES3.1 deferred it. /be ___ Es4-discuss mailing list Es4-discuss@mozilla.org https://mail.mozilla.org/listinfo/es4-discuss
Re: function hoisting like var
Igor Bukanov wrote: 2008/7/26 Ingvar von Schoultz [EMAIL PROTECTED]: Do you really think {{ }} appears in existing code, correctly enclosing statement blocks, with the {{ and the }} placed tightly together both at the beginning and at the end? Yes: I have seen the code like if (x) { { code } } Now pass through a JS compressor that removes redundant whitespace and you get precizelly {{ }}. In addition, those {{ can happen in a code generator. Regards, Igor ___ Es4-discuss mailing list Es4-discuss@mozilla.org https://mail.mozilla.org/listinfo/es4-discuss How sad! It seemed such a simple and intuitive notation! I think all of these would be unambiguous: {. code .} {: code :} {| code |} {[ code ]} [[ code ]] [ code ] -- Ingvar von Schoultz --- (My quirky use of capitals in code comes from my opinion that reserved and predefined words should all start with lowercase, and user-defined should all start with uppercase, because this will easily and elegantly prevent a host of name-collision problems when things like programming languages are upgraded with new labels.) ___ Es4-discuss mailing list Es4-discuss@mozilla.org https://mail.mozilla.org/listinfo/es4-discuss
Re: function hoisting like var
Brendan Eich wrote: Waldemar meant precisely what he wrote: ES3 and draft ES3.1 -- the specifications, not random JS implementations. Oops, I got lost in details and strayed far away from the point that I wanted to make. In fact I should have said this from the beginning: You can't get away from supporting this: { function a(){} var b = a; } ES4 is planning to support function declarations locally bound in blocks, so the above is valid ES4 code. What you see above is function b() hoisting like var. (I said b, not a.) There is no far-too-complicated split-scope complexity. There is no capturing of variables that haven't been declared yet. It's simple, intuitive, well-defined and well-behaved. The above is the /exact/ functionality of function hoisting like var, apart from using two names. You can refuse the clearer syntax, but you can't refuse the above code and functionality. In other words, complexity is not a problem. ES4 can easily choose whatever semantics people prefer. -- Ingvar von Schoultz --- (My quirky use of capitals in code comes from my opinion that reserved and predefined words should all start with lowercase, and user-defined should all start with uppercase, because this will easily and elegantly prevent a host of name-collision problems when things like programming languages are upgraded with new labels.) ___ Es4-discuss mailing list Es4-discuss@mozilla.org https://mail.mozilla.org/listinfo/es4-discuss
Re: function hoisting like var
2008/7/26 Ingvar von Schoultz [EMAIL PROTECTED]: I think all of these would be unambiguous: {. code .} {. not work since {.0; } is a valid ES3 {: code :} {| code |} These 2 cases are indeed invalid ES3. {[ code ]} [[ code ]] [ code ] Any of this can be a valid ES3 code as [] always means an array literal. Regards, Igor ___ Es4-discuss mailing list Es4-discuss@mozilla.org https://mail.mozilla.org/listinfo/es4-discuss
Re: function hoisting like var
On Jul 26, 2008, at 2:07 PM, Ingvar von Schoultz wrote: You can't get away from supporting this: { function a(){} var b = a; } ES4 is planning to support function declarations locally bound in blocks, so the above is valid ES4 code. What you see above is function b() hoisting like var. (I said b, not a.) What you said does not make sense. It's true that var b is hoisted to the top of the program or function body. But it is not initialized until control flows through the assignment b = a that is part of the var declaration. So there is no capture problem. There is no far-too-complicated split-scope complexity. There is no capturing of variables that haven't been declared yet. It's simple, intuitive, well-defined and well-behaved. Thanks, I agree. But it is not what you proposed. Again, from Waldemar's original reply, but with your proposed {{}} interpolated and the elided code amended to say what the consequence is: // outer scope function c() ...; // inner scope {{ if (foo) { const c = 37; } ... c in your proposal must be hoisted to the {{, so it can't be function c -- yet it can't be initialized to 37 if foo is falsy ... }} You could reply that const is new (sort of -- two browsers already implement it one way, another treats it as var) and therefore should always scope to { or {{, whichever is closer. But the point stands if you replace const with function or var and hoist to the {{. Repeating the next counter-example, with {{}} changes again, to track your proposal since the original exchange with Waldemar: // outer scope function c() ...; // inner scope {{ function f() { return c; } a = f(); if (foo) { const c = 37; } b = f(); ... just what do a and b hold here? Was f's captured variable rebound by the if statement? ... }} And so on. The above is the /exact/ functionality of function hoisting like var, apart from using two names. You can refuse the clearer syntax, but you can't refuse the above code and functionality. I think I see the confusion now. Do you believe that in the var b = a; code you wrote, both the binding of the var named b *and* its initialization with the value of the function object denoted a are hoisted? Hoisted up to what point? Waldemar wrote a while back: Keep in mind that function assignments hoist to the beginning of the scope in which the function is defined, so your proposal won't work. The word assignment where definition was perhaps more precise (function definitions replace extant properties of the same name in the variable object, they are not equivalent to assignment expressions) may have misled you. From the context and the long- standing spec and implementation behavior with functions not in blocks or any other sub-statement position, it was clear (I think) what was meant, but I can see how this could be confusing. Assignment expressions and initializers in var statements do not hoist or otherwise move in the flow of control. /be ___ Es4-discuss mailing list Es4-discuss@mozilla.org https://mail.mozilla.org/listinfo/es4-discuss
Re: function hoisting like var
On Jul 26, 2008, at 2:06 PM, Ingvar von Schoultz wrote: How sad! It seemed such a simple and intuitive notation! Opinions vary, but all the ones I heard at the Ecma TC39 meeting found it neither simple nor intuitive, and some abhorred it on aesthetic grounds to boot. I think all of these would be unambiguous: {. code .} {: code :} {| code |} {[ code ]} [[ code ]] [ code ] These are either syntax errors without opt-in versioning, or (the last three) do create incompatible ambiguity (consider array initialisers). What's more, as Waldemar pointed out many threads (and too many words) ago, they create capture problems. Please work through the last mail I sent before replying; if some vocabulary or infelicitous word choice is causing any confusion, feel free to mail me privately and ask pointed questions. Thanks, /be ___ Es4-discuss mailing list Es4-discuss@mozilla.org https://mail.mozilla.org/listinfo/es4-discuss
Re: function hoisting like var
don't see any problem or disadvantage with that uninitialized c. It's what the programmer coded. It's also how |var| works. In my opinion, |var| and |const| should have exactly the same behavior, except for the constantness. It looks perfectly fine to me. So please explain. Repeating the next counter-example, with {{}} changes again, to track your proposal since the original exchange with Waldemar: // outer scope function c() ...; // inner scope {{ function f() { return c; } a = f(); if (foo) { const c = 37; } b = f(); ... just what do a and b hold here? Was f's captured variable rebound by the if statement? ... }} c is a constant that is visible throughout the inner scope {{ }}, visible from before you enter the scope and throughout. It shadows the outer function c() throughout. It is initially unassigned, or assigned |undefined|. When you call |a = f()|, the function f() accesses this unassigned c. I don't know if there's a consensus about what should happen when you access an unassigned constant. It seems to me that this should raise an error. But my only reason for saying this is that if it doesn't, the constant becomes a one-shot binary toggle, and a toggle isn't a constant. So it's a detail of intuitive semantics and consistency. If instead it's specified that an unassigned constant returns |undefined|, then a will contain |undefined| and the program continues. Then |if (foo)| may or may not allow c to get its only-once-permitted assignment. I don't know why you mention rebinding. Maybe I've misunderstood something. But as I see it, it's just an assignment, just as if c were a var, except you can only assign once. You ask how f() sees it (I think). Well, f() sees it just like it sees any var in that scope. I'm not sure this answers your question, I hope it does. After that, b will receive whatever c contains, either |undefined| or 37. A different notion of constant is possible, where earlier assignment is enforced, or maybe even hoisted to the beginning of the scope. But I think the constant that I described is a /much/ better fit for this language. I hope the above description is sufficient. What's this talk about rebinding? Waldemar asked about rebinding, and unless I misunderstood, rebinding was at the basis of the strange things that he thought that I was proposing. I have not intended to propose any kind of rebinding. If there is any kind of rebinding inherent in any one of my proposals, I'm not aware of it. Maybe I'm misunderstanding something. I can't say anything more than that about this rebinding for now, but I can say this. Think about var declarations: alert (a); var a = 10; alert (a); This alerts undefined and 10. If you remove |var a = 10| it will instead raise an error. That's because the name a is then unknown. So when you use |var a| anywhere within the scope, the name a becomes visible both before and after |var a|. |var| isn't something executable (in my mind model at least), it just says which scope the variable resides in. The result is this scoping. And as such, it is valid throughout the scope. This is a very good arrangement, and I think all declarations should work this way, always, consistently (all declarations where this makes sense). In the following example, one var is redundant. You can omit either one, the meaning remains identical: function f() { var a = 10; var a = 20; } Here's another example of redundant var: if (x) var a = 10; else var a = 20; For consistency and simplicity, in my opinion all declarations should also allow this redundancy. It's an odd arrangement, but in fact it's useful. Of course such redundant declarations must be consistent. So the following would be a conflicting declaration error: if (x) var a = 10; // ERROR -- Conflicts with const. else const a = 20; // ERROR -- Conflicts with var. I use redundant |var| everywhere in my code to signal localness. I do this because if I leave out |var|, this is a clear warning to myself that I'm accessing something external. I would greatly prefer having a strict mode that required me to use an |outer| declaration, as a much clearer warning that I'm accessing something external: var a = 10, b = 20; function f() { outer a; c = outer b; } This would be /much/ more useful than data types in very small programs. And so on. The above is the /exact/ functionality of function hoisting like var, apart from using two names. You can refuse the clearer syntax, but you can't refuse the above code and functionality. I think I see the confusion now. Do you believe that in the var b = a; code you wrote, both the binding of the var named b *and* its initialization with the value of the function object denoted a are hoisted? Hoisted up to what point? No, it starts out |undefined|, and that's
Re: function hoisting like var
Ingvar von Schoultz wrote: [EMAIL PROTECTED] wrote: I'm trying to keep the language relatively simple. You can't get away from supporting this: { function a(){} var b = a; } What do you mean? This is a syntax error in both ES3 and ES3.1. On the contrary, the functionality already exists, as shown above. It does not already exist in ES3 or ES3.1. Keep in mind that function assignments hoist to the beginning of the scope in which the function is defined, so your proposal won't work. When the programmer explicitly says that the assignment depends on sequential code, then do what the programmer says. Anything else is an error. Do it by assigning |undefined| before scope entry. This is the only correct thing to do. I don't understand. You're trying to do a complex split-scope approach where each function definition has *two* scopes, one in which it is declared and one in which it is defined, but even that won't work with const, typed functions and variables, etc. Are you saying that the function /body/ gets into trouble, or the function /name/? Both. The function /body/ stays where it is. The hoisting doesn't affect it in any way. Moving the body would change its context and meaning catastrophically. Don't touch it. That doesn't answer the question. The problem is that the body can capture variables that haven't been declared yet. The /name/ becomes a var. Treat it like any other var. Hoist it and assign |undefined|, exactly like you do with other vars. That's incompatible with how functions are defined in ES3. Are you saying that because this var is related to a function it can't be treated like other vars? Is this var fundamentally different from other vars? At least above it isn't. Huh? Do you understand how ES3 works? That email is about some wildly unworkable dynamic scoping. It has nothing to do with anything I ever said. You jumped to that conclusion. Please stop insisting that I'm proposing that nonsense. I'm not. I never did. I haven't seen a sensible and compatible proposal yet. You'd then have to introduce extra rules about some definitions only being possible within {{}} blocks, which would then affect the behavior of existing definitions like var if one of the other definitions within the same block was a const or function, which would snowball into a complex mess. Unrelated, I believe. So you're saying that your {{}} proposal has nothing to do with this? Waldemar ___ Es4-discuss mailing list Es4-discuss@mozilla.org https://mail.mozilla.org/listinfo/es4-discuss
Re: function hoisting like var
I guess that proposal can be summarized in a very short form: make {{ code }} a syntax sugar for (function() { code })() On a few occasions I have used the latter form in ES3 programs to get the benefits of the let locals without using the let keyword. Regards, Igor ___ Es4-discuss mailing list Es4-discuss@mozilla.org https://mail.mozilla.org/listinfo/es4-discuss
RE: function hoisting like var
Is {{ return }} syntactic sugar for (function() {{ return }})() too? --lars -Original Message- From: [EMAIL PROTECTED] [mailto:es4-discuss- [EMAIL PROTECTED] On Behalf Of Igor Bukanov Sent: 25. juli 2008 13:34 To: [EMAIL PROTECTED] Cc: [EMAIL PROTECTED]; es4-discuss@mozilla.org; Ingvar von Schoultz Subject: Re: function hoisting like var I guess that proposal can be summarized in a very short form: make {{ code }} a syntax sugar for (function() { code })() On a few occasions I have used the latter form in ES3 programs to get the benefits of the let locals without using the let keyword. Regards, Igor ___ Es4-discuss mailing list Es4-discuss@mozilla.org https://mail.mozilla.org/listinfo/es4-discuss ___ Es4-discuss mailing list Es4-discuss@mozilla.org https://mail.mozilla.org/listinfo/es4-discuss
Re: function hoisting like var
2008/7/25 Lars Hansen [EMAIL PROTECTED]: Is {{ return }} syntactic sugar for (function() {{ return }})() too? I have forgot about the return or break/continue. But this is not a point since a syntax sugar for (function() { code })() can require that code must not contain return or break/continue outside the code effectively making it a let expression on steroids. The point is that is it worth to provide a shortcut for (function() { code })() since that is used on web. Of cause, if desired, such shortcut should be pure syntax extension over ES3, not {{ }} as that silently changes the meaning of the existing code. One way to get that extension comes from an observation that ES4 allows to drop the braces around function body and the return keyword if the body is the single return exp. If ES3.1 would support it, then to simulate an effect of let expression like let (a = arg1, b = arg2) expression one could write (function(a, b) expression)(arg1, arg2) or (function(a, b) a = arg1, b = arg2, expression)() If, in addition to this shortcut, ES4 would allow to drop an empty argument list from a function with a requirement that function { } always means an expression and ES3.1 would also support that, then in place of a block with let variables one can write: function { }(); Regards, Igor ___ Es4-discuss mailing list Es4-discuss@mozilla.org https://mail.mozilla.org/listinfo/es4-discuss
Re: function hoisting like var
Igor Bukanov wrote: I guess that proposal can be summarized in a very short form: make {{ code }} a syntax sugar for (function() { code })() Indeed you're right. When I proposed {{ }} my intent was simplicity, but I didn't realize that implementing it could be made that simple, the change that small. By the way, your translation (function() { code })() can also be expressed as new function() { code } Less parentheses! (But I haven't explored possible drawbacks.) -- Ingvar von Schoultz --- (My quirky use of capitals in code comes from my opinion that reserved and predefined words should all start with lowercase, and user-defined should all start with uppercase, because this will easily and elegantly prevent a host of name-collision problems when things like programming languages are upgraded with new labels.) ___ Es4-discuss mailing list Es4-discuss@mozilla.org https://mail.mozilla.org/listinfo/es4-discuss
Re: function hoisting like var
Igor Bukanov wrote: Of cause, if desired, such shortcut should be pure syntax extension over ES3, not {{ }} as that silently changes the meaning of the existing code. Do you really think {{ }} appears in existing code, correctly enclosing statement blocks, with the {{ and the }} placed tightly together both at the beginning and at the end? I find this somewhat unlikely, since it's a redundant repetition that means exactly the same as { }. Ingvar Igor Bukanov wrote: 2008/7/25 Lars Hansen [EMAIL PROTECTED]: Is {{ return }} syntactic sugar for (function() {{ return }})() too? I have forgot about the return or break/continue. But this is not a point since a syntax sugar for (function() { code })() can require that code must not contain return or break/continue outside the code effectively making it a let expression on steroids. The point is that is it worth to provide a shortcut for (function() { code })() since that is used on web. Of cause, if desired, such shortcut should be pure syntax extension over ES3, not {{ }} as that silently changes the meaning of the existing code. One way to get that extension comes from an observation that ES4 allows to drop the braces around function body and the return keyword if the body is the single return exp. If ES3.1 would support it, then to simulate an effect of let expression like let (a = arg1, b = arg2) expression one could write (function(a, b) expression)(arg1, arg2) or (function(a, b) a = arg1, b = arg2, expression)() If, in addition to this shortcut, ES4 would allow to drop an empty argument list from a function with a requirement that function { } always means an expression and ES3.1 would also support that, then in place of a block with let variables one can write: function { }(); Regards, Igor ___ Es4-discuss mailing list Es4-discuss@mozilla.org https://mail.mozilla.org/listinfo/es4-discuss -- Ingvar von Schoultz --- (My quirky use of capitals in code comes from my opinion that reserved and predefined words should all start with lowercase, and user-defined should all start with uppercase, because this will easily and elegantly prevent a host of name-collision problems when things like programming languages are upgraded with new labels.) ___ Es4-discuss mailing list Es4-discuss@mozilla.org https://mail.mozilla.org/listinfo/es4-discuss
Re: function hoisting like var
please try to be more specific? What specifically is the problem with my question? Asking if I understand ES3 really isn't in any way specific. Please stop insisting that I'm proposing that nonsense. I'm not. I never did. I haven't seen a sensible and compatible proposal yet. That's because you read wild ideas into my code snippets and then assume that that's my proposal. I'm always wondering if your vague protests, usually ending in so your proposal won't work, are about one of my real proposals (and if so, which one), or if you have once again looked at one of my code snippets and built theories about me wanting to redefine the fundamentals of the language because there's an if() before a var. I'm quite astonished. I've read more than half the discussion archives of ES3.1 and ES4. All other discussions seem serious. You have some special problem with me. I seems it started when you misunderstood that code snippet. I care a lot about JavaScript, and have collected several ideas over the years. Some of them could be quite useful. But I doubt that I can present an idea here and expect it to be taken for what it is. The reason I'm reading the discussion archives so much is that I want to see which ones among my ideas can be useful. I do this because I care about contributing constructively. I'm serious about such things. Everywhere else I'm well respected. This is a new situation for me. It feels unreal. You'd then have to introduce extra rules about some definitions only being possible within {{}} blocks, which would then affect the behavior of existing definitions like var if one of the other definitions within the same block was a const or function, which would snowball into a complex mess. Unrelated, I believe. So you're saying that your {{}} proposal has nothing to do with this? Yes. We're discussing hoisting to a different scope. With {{ }} there is no hoisting to a different scope. Everything stays in the scope where it's declared. Igor Bukanov made a nice summary by pointing out that {{ code }} becomes syntax sugar for (function() { code })(). Indeed, if you write the latter using ES3, without any scope blocks and let declarations, that's exactly the scoping that you get with {{ }}. And this shows that the {{ }} proposal is unrelated to function hoisting like var. In that context there are no scope blocks that var will hoist out of. -- Ingvar von Schoultz --- (My quirky use of capitals in code comes from my opinion that reserved and predefined words should all start with lowercase, and user-defined should all start with uppercase, because this will easily and elegantly prevent a host of name-collision problems when things like programming languages are upgraded with new labels.) ___ Es4-discuss mailing list Es4-discuss@mozilla.org https://mail.mozilla.org/listinfo/es4-discuss
function hoisting like var -- Was: Re: Surprising semantics
[EMAIL PROTECTED] wrote: I'm trying to keep the language relatively simple. You can't get away from supporting this: { function a(){} var b = a; } What you see above is function b() hoisting like var. This is the /exact/ functionality, apart from using two names. You can refuse the clearer syntax, but you can't refuse the above code and functionality. b is bound to the global scope and assigned |undefined| before you enter the global scope. Assigning |undefined| is correct for any function whose assignment depends on sequential code. The above is such a sequential dependency, even though it may not look that way. What you're proposing is far too complicated. On the contrary, the functionality already exists, as shown above. Keep in mind that function assignments hoist to the beginning of the scope in which the function is defined, so your proposal won't work. When the programmer explicitly says that the assignment depends on sequential code, then do what the programmer says. Anything else is an error. Do it by assigning |undefined| before scope entry. This is the only correct thing to do. You're trying to do a complex split-scope approach where each function definition has *two* scopes, one in which it is declared and one in which it is defined, but even that won't work with const, typed functions and variables, etc. Are you saying that the function /body/ gets into trouble, or the function /name/? The function /body/ stays where it is. The hoisting doesn't affect it in any way. Moving the body would change its context and meaning catastrophically. Don't touch it. The /name/ becomes a var. Treat it like any other var. Hoist it and assign |undefined|, exactly like you do with other vars. Are you saying that because this var is related to a function it can't be treated like other vars? Is this var fundamentally different from other vars? At least above it isn't. Or is the problem in the type declarations? Are datatypes of vars and functions fundamentally different? Or is it because functions have parameters? You're not saying anything about parameters, so if that's the problem you're being very vague indeed. See my previous email as to why. That email is about some wildly unworkable dynamic scoping. It has nothing to do with anything I ever said. You jumped to that conclusion. Please stop insisting that I'm proposing that nonsense. I'm not. I never did. You'd then have to introduce extra rules about some definitions only being possible within {{}} blocks, which would then affect the behavior of existing definitions like var if one of the other definitions within the same block was a const or function, which would snowball into a complex mess. Unrelated, I believe. Ingvar Ingvar von Schoultz wrote: I'm astonished that you interpreted my text in such a weird way! That's completely foreign to JavaScript! var is always unconditional in JavaScript. An if() before a declaration doesn't make the declaration conditional. The var takes effect long before you reach that if(). It takes effect before you enter the scope in which the variable resides. You can consider the declaration glued to the opening brace of that scope. Or better, glued to both braces and stretched between them. The assignment, on the other hand, stays in place and is conditional. But only the assignment. With your surprising interpretation things would be much worse than what your examples suggest. Much worse. Consider assigning to the unpredictable variable: function Outer() { var Test = 1; function Inner() { if (Unknown) var Test = 2; // Weird conditional declaration // Many // lines // of // code Test = 3; // Semantics totally unpredictable } } Is the outer Test set to three, or has an inner Test been created that can take the value? Every program would become impossible to understand, an unintelligible mess. With my subject line surprising semantics I didn't mean to advocate more surprises, I wanted less! I assumed that ES3.1 and ES4 declarations would work like ES3 declarations do: The declaration spans the entire scope from before you enter the scope and throughout. The value is unassigned (undefined) until you reach an assignment. I took it for granted that making it constant and/or giving it a type would follow the same pattern. The name gets associated with the constantness and/or type before you enter the scope, and it's in effect throughout. In your examples with dual scopes, the result depends on whether you mean that the inner c is bound to the outer scope or the inner. If it's the outer you have conflicting, irreconcilable bindings that collide before you enter that outer scope, a clear declaration error. If it's the inner, then the inner declaration shadows the outer, so