Existential operator (was: ||= is much needed?)
John Tamplin
jat at google.com
Wed Jun 20 07:18:09 PDT 2012
On Tue, Jun 19, 2012 at 1:34 PM, Jeremy Ashkenas <jashkenas at gmail.com>wrote:
> Allen asks about when you would want to write `foo?.bar()`
>
> ... the only use case I can think of is when `foo` may or may not be
> defined, and you know that if it *is* defined, it's going to have a `bar`
> method on it, and if it *is* defined, you want to call `bar` for its side
> effects. A bit far fetched.
>
It doesn't seem that far-fetched to me -- I seem to constantly write things
like this in Java:
String name = (person != null) ? person.getName() : null;
IIUC, that could just be written as String name = person?.getName() if this
operator were avaialble.
Maybe you are saying that you don't want to have rigid class structures in
JS, but it seems reasonable that if you know person is of type Person it is
supposed to have a getName method on it.
--
John A. Tamplin
Software Engineer (GWT), Google
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120620/9d935b2d/attachment.html>
More information about the es-discuss
mailing list