Existential Operator / Null Propagation Operator

Kevin Smith zenparsing at gmail.com
Fri Apr 24 19:15:52 UTC 2015


I think it's a good idea to attempt to express these ideas using existing
syntax and see what that might look like.

So, for a bit of fun, I wrote this:

https://gist.github.com/zenparsing/9ff3036b6eb15fa436e4

Basically, there's a Maybe function which returns a proxy over a target,
and returns Maybes for calls and gets.  Then there's a Definitely function
that unwraps the Maybe (unwrapping Nothing to undefined of course).

    let scriptParent =
Definitely(Maybe(window).document.scripts[0].parentNode);

If you wanted syntactic sugar for this kind of thing, you'd probably want
postfix operators to replace the Maybe and Definitely function calls.

Swift does something similar with "?" and "!":

https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/OptionalChaining.html

This is kind of a generic monad pattern though, so it would be cool if the
syntax could be applied to other monad-ish things.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150424/bcdcec43/attachment-0001.html>


More information about the es-discuss mailing list