<div>David,</div><div><br></div><div>The reason Parenscript doesn't ship with macros for this kind of thing</div><div>is, I suppose, that they're so easy to write and everyone's preferences</div><div>are slightly different. Since we have</div>
<div><br></div><div> (@ addr 'zipcode) => addr.zipcode</div><div><br></div><div>you could imagine something like this to add in the null check:</div><div><br></div><div> (? addr 'zipcode) => (address != null) ? address.zipcode : null</div>
<div><br></div><div>The trickiest part really is figuring out what notation you</div><div>want to use. I don't know if ? is a good notation or not, it's</div><div>just the first that occurred to me.</div><div><br>
</div><div>That being said, there is a structural issue with s-expressions not</div><div>being so good for chaining such expressions together, since each</div><div>accessor introduces a new level of nesting. This isn't readable</div>
<div>when you have more than two or three properties in the chain:</div><div><br></div><div> (@ (@ (@ lottery 'winner) 'address) 'zipcode) </div><div> => lottery.winner.address.zipcode</div><div><br></div>
<div>Parenscript gets around that by allowing (@ lottery 'winner 'address 'zipcode) </div><div>as shorthand for (@ (@ (@ lottery 'winner) 'address) 'zipcode). By</div><div>analogy, you could have (? lottery 'winner 'address 'zipcode) as</div>
<div>shorthand for (? (? (? lottery 'winner) 'address) 'zipcode), which</div><div>would add a null check before each access. But I don't see any way</div><div>to represent the equivalent of lottery.drawWinner?().address?.zipcode</div>
<div>in a flat way in s-expressions, since it's not using the same access</div><div>operator all the way through.</div><div><br></div><div>Arguably the best solution would be to extend the PS reader with</div><div>syntax for the dot operator, so lottery.winner.address.zipcode would</div>
<div>be syntax for (@ (@ (@ lottery 'winner) 'address) 'zipcode). Then, PS</div><div>being a Lisp, it would presumably not be very hard to add one's own</div><div>extensions like ? for null-checking. So far, though, no one has</div>
<div>undertaken such work on the PS reader. (Early versions of PS</div><div>actually had dot syntax for property access, but not at the </div><div>reader level. This wasn't convenient for many purposes, especially</div>
<div>macro writing, since an expression like lottery.winner.address.zipcode</div><div>was just one big lump of a symbol and you had to parse it yourself.)</div><div><br></div><div>Daniel</div><div><br></div><br><div class="gmail_quote">
On Sat, Jan 19, 2013 at 6:30 PM, David Sargeant <span dir="ltr"><<a href="mailto:david@dsargeant.com" target="_blank">david@dsargeant.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div style="word-wrap:break-word"><div style="margin:0px;font-size:16px">It would be great to be able to do "soaks" like in CoffeeScript. Example from <a href="http://coffeescript.org" target="_blank">coffeescript.org</a>:</div>
<div style="margin:0px;font-size:16px;min-height:19px"><br></div><div style="margin:0px;font-size:12px;font-family:Courier">zip = lottery.drawWinner?().address?.zipcode</div><div style="margin:0px;font-size:12px;font-family:Courier;min-height:14px">
<br></div><div style="margin:0px;font-size:12px;font-family:Courier">Produces</div><div style="margin:0px;font-size:12px;font-family:Courier;min-height:14px"><br></div><div style="margin:0px;font-size:12px;font-family:Courier">
var zip, _ref;</div><div style="margin:0px;font-size:12px;font-family:Courier;min-height:14px"><br></div><div style="margin:0px;font-size:12px;font-family:Courier">zip = typeof lottery.drawWinner === "function" ? (_ref = lottery.drawWinner().address) != null ? _ref.zipcode : void 0 : void 0;</div>
<div><br></div></div><br>_______________________________________________<br>
parenscript-devel mailing list<br>
<a href="mailto:parenscript-devel@common-lisp.net">parenscript-devel@common-lisp.net</a><br>
<a href="http://lists.common-lisp.net/cgi-bin/mailman/listinfo/parenscript-devel" target="_blank">http://lists.common-lisp.net/cgi-bin/mailman/listinfo/parenscript-devel</a><br>
<br></blockquote></div><br>