P T Withington wrote:
On 2011-04-14, at 11:55, John J. Barton wrote:

  
Perhaps there is no better solution, but often I find that I want to say "call this chain of functions and use the bottom value, but if any of them return undefined, then just be undefined, don't get all pissed off and throw an exception. Being undefined is how _javascript_ is." I was imagining that this was the feature being discussed.
    
This is the way AS2 worked.  If at any point an undefined value was hit (whether property access or call), the evaluation stopped and undefined was returned.  The experience of OpenLaszlo was that this led to many myserious bugs.  We ended up making our compiler insert annotations in debug mode to detect and warn when an _expression_ failed due to an undefined value.  That experience makes me believe that the current behavior in ES is a better default; but having a shorthand for "stop now if this is undefined", seems like it would be a nice addition.
  
Well, it's too bad you did not have a better debugger then ;-).

Based on your solution, the problem isn't the statement or its syntax.  At runtime you ended up with an 'undefined'. You did not expect it: there is a bug in some method in the chain that resulted in 'undefined': it should *not* have returned 'undefined' when it did.

Given that you have buggy code, what is the best syntax?

You could make the argument for syntax that throws:
    try {
       var maybe = foo().bar().baz(); // one will fail, we have a bug
     } catch (exc) {
     }
You could make the argument for defensive programming:
    var dull = foo();
    if (dull)
          var duller = dull.bar();
    if (duller)
          var dullest = duller. baz();

But given that we have already decided that the use case is "be undefined if your chain is undefined", these are just long winded.

So you could make the argument that the Dmitry thing ought to work with the debugger. Rather than a mysterious 'undefined' you should have a a way to rapidly ask  the debugger to tell you: which link in the chain is 'undefined'. And that is what you did in your compiler: a good solution to your problem but not an argument against the syntax IMO.

jjb


_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to