[racket] Style or and/define

From: Laurent (laurent.orseau at gmail.com)
Date: Tue Jun 11 13:15:57 EDT 2013

On Tue, Jun 11, 2013 at 7:11 PM, Sean McBeth <sean.mcbeth at gmail.com> wrote:

> 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
>
> (define (get-x-spot char-width)
>   (define dc (and char-width (get-dc)))
>   (define style (and dc (or (send (get-style-list) find-named-style
> "Standard")
>                             (send (get-style-list) find-named-style
> "Basic"))))
>   (define fnt (and style (send style get-font)))
>
>   (when fnt
>     (define-values (xw _1 _2 _3) (send dc get-text-extent "x" fnt))
>     (+ left-padding (* xw char-width)))
>

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).
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.

Laurent



>
> On Tue, Jun 11, 2013 at 1:04 PM, Sean McBeth <sean.mcbeth at gmail.com>wrote:
>
>> Would let* mostly achieve this?
>>
>> (define (get-x-spot char-width)
>>   (let* ([dc (and char-width (get-dc))]
>>          [style (and dc (or (send (get-style-list) find-named-style
>> "Standard")
>>                             (send (get-style-list) find-named-style
>> "Basic")))]
>>          [fnt (and style (send style get-font))])
>>     (when fnt
>>
>>       (define-values (xw _1 _2 _3) (send dc get-text-extent "x" fnt))
>>        (+ left-padding (* xw char-width)))))
>>
>>
>> On Tue, Jun 11, 2013 at 12:49 PM, Laurent <laurent.orseau at gmail.com>wrote:
>>
>>> When I see what Robby is forced to write when following the Style:
>>>
>>> https://github.com/plt/racket/commit/09d636c54573522449a6591c805b38f72b6f7da8#L4R963
>>>
>>> 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).
>>> Using `let' and `and' instead, although being a bit better since it
>>> avoids all the [else #f], is not that big an improvement:
>>>
>>> (define (get-x-spot char-width)
>>>   (and
>>>    char-width
>>>    (let ([dc (get-dc)])
>>>      (and
>>>       dc
>>>       (let ([style (or (send (get-style-list) find-named-style
>>> "Standard")
>>>                        (send (get-style-list) find-named-style
>>> "Basic"))])
>>>         (and
>>>          style
>>>          (let*-values ([(fnt) (send style get-font)]
>>>                        [(xw _1 _2 _3) (send dc get-text-extent "x" fnt)])
>>>            (+ left-padding (* xw char-width)))))))))
>>>
>>>
>>> Actually I think here the right thing to do might be to allow for
>>> internal definitions inside `and':
>>>
>>> (define (get-x-spot char-width)
>>>   (and char-width
>>>        (define dc (get-dc))
>>>        dc
>>>        (define style (or (send (get-style-list) find-named-style
>>> "Standard")
>>>                          (send (get-style-list) find-named-style
>>> "Basic")))
>>>        style
>>>        (define fnt (send style get-font))
>>>        (define-values (xw _1 _2 _3) (send dc get-text-extent "x" fnt))
>>>        (+ left-padding (* xw char-width))))
>>>
>>>
>>> Isn't it *much* more readable? (shorter, avoid rightward drift, less
>>> parens, vertical alignment)
>>>
>>> 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?
>>>
>>> I've played a bit with it if you want to try by your own:
>>> https://gist.github.com/Metaxal/5758394
>>>
>>> (not sure I got it all good with syntax-parse though)
>>>
>>> Laurent
>>>
>>>
>>> ____________________
>>>   Racket Users list:
>>>   http://lists.racket-lang.org/users
>>>
>>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20130611/249fa7c7/attachment.html>

Posted on the users mailing list.