<div dir="ltr"><br><div class="gmail_extra"><br><br><div class="gmail_quote">On Tue, Jun 11, 2013 at 7:11 PM, Sean McBeth <span dir="ltr"><<a href="mailto:sean.mcbeth@gmail.com" target="_blank">sean.mcbeth@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>Okay, there is one difference between our examples, in that yours will short-circuit and mine won't. However, if that is not a concern and you're looking more for readability, you could even use local define<br>
<br><span style="font-family:courier new,monospace">(define (get-x-spot char-width)<br> (define dc (and char-width (get-dc)))<br> (define style (and dc (or (send (get-style-list) find-named-style "Standard")<br>
(send (get-style-list) find-named-style "Basic"))))<br> (define fnt (and style (send style get-font)))<div class="im"><br> (when fnt<br> (define-values (xw _1 _2 _3) (send dc get-text-extent "x" fnt))<br>
(+ left-padding (* xw char-width)))</div></span></div></div></blockquote><div><br></div><div>I do feel not short-circuiting is an issue, because then you have to read the whole code to understand that if char-width is false, all is false (and same for the others).<br>
</div><div>Furthermore your `when' returns `(void)' and not `#f', so the code is not
equivalent here, and I suspect you've written it this way exactly because you can't use `and' easily with the multiple values.<br><br></div><div>Laurent<br></div><div><br> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div class=""><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Jun 11, 2013 at 1:04 PM, Sean McBeth <span dir="ltr"><<a href="mailto:sean.mcbeth@gmail.com" target="_blank">sean.mcbeth@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Would let* mostly achieve this?<br><br><span style="font-family:courier new,monospace">(define (get-x-spot char-width)<br>
(let* ([dc (and char-width (get-dc))]<br> [style (and dc (or (send (get-style-list) find-named-style "Standard")<br>
(send (get-style-list) find-named-style "Basic")))]<br> [fnt (and style (send style get-font))])<br> (when fnt<div><br> (define-values (xw _1 _2 _3) (send dc get-text-extent "x" fnt))<br>
</div>
(+ left-padding (* xw char-width)))))</span><br></div><div class="gmail_extra"><br><br><div class="gmail_quote"><div><div>On Tue, Jun 11, 2013 at 12:49 PM, Laurent <span dir="ltr"><<a href="mailto:laurent.orseau@gmail.com" target="_blank">laurent.orseau@gmail.com</a>></span> wrote:<br>
</div></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><div><div dir="ltr"><div><div><div><div><div><div><div>When I see what Robby is forced to write when following the Style:<br>
<a href="https://github.com/plt/racket/commit/09d636c54573522449a6591c805b38f72b6f7da8#L4R963" target="_blank">https://github.com/plt/racket/commit/09d636c54573522449a6591c805b38f72b6f7da8#L4R963</a><br>
</div><br>I cannot help but think that something is wrong somewhere (it may not be the Style, and in case it wasn't clear I'm certainly not criticizing Robby's code).<br></div><div></div>Using `let' and `and' instead, although<span style="font-family:courier new,monospace"><span style="font-family:arial,helvetica,sans-serif"> being a bit better since it avoids all the [else #f], is not that big an improvement:</span><br>
<br>(define (get-x-spot char-width)<br> (and<br> char-width <br> (let ([dc (get-dc)])<br> (and<br> dc<br> (let ([style (or (send (get-style-list) find-named-style "Standard")<br> (send (get-style-list) find-named-style "Basic"))])<br>
(and<br> style<br> (let*-values ([(fnt) (send style get-font)]<br> [(xw _1 _2 _3) (send dc get-text-extent "x" fnt)])<br> (+ left-padding (* xw char-width)))))))))</span><br>
<br><br></div>Actually I think here the right thing to do might be to allow for internal definitions inside `and':<br><br><span style="font-family:courier new,monospace">(define (get-x-spot char-width)<br> (and char-width<br>
(define dc (get-dc))<br> dc<br> (define style (or (send (get-style-list) find-named-style "Standard")<br> (send (get-style-list) find-named-style "Basic")))<br>
style<br> (define fnt (send style get-font))<br> (define-values (xw _1 _2 _3) (send dc get-text-extent "x" fnt))<br> (+ left-padding (* xw char-width))))<br></span><br><br></div>Isn't it *much* more readable? (shorter, avoid rightward drift, less parens, vertical alignment)<br>
<br></div><div>Since it's not the first time I find the need for such internal definitions in `and', maybe this is something to consider for future addition to Racket? Or have some people already identified some problems with this idea?<br>
</div><div><br></div>I've played a bit with it if you want to try by your own:<br><a href="https://gist.github.com/Metaxal/5758394" target="_blank">https://gist.github.com/Metaxal/5758394</a><br><br></div>(not sure I got it all good with syntax-parse though)<span><font color="#888888"><br>
<br></font></span></div><span><font color="#888888">Laurent<br><div><div><br></div></div></font></span></div>
<br></div></div>____________________<br>
Racket Users list:<br>
<a href="http://lists.racket-lang.org/users" target="_blank">http://lists.racket-lang.org/users</a><br>
<br></blockquote></div><br></div>
</blockquote></div><br></div>
</div></div></blockquote></div><br></div></div>