[racket] syntax, differently

From: Laurent (laurent.orseau at gmail.com)
Date: Wed Aug 18 12:26:24 EDT 2010

On Wed, Aug 18, 2010 at 16:52, Matthias Felleisen <matthias at ccs.neu.edu>wrote:

> Eduardo is providing a macro that helps you abbreviate complex
> selector and mutator chains. The BENFIT you get is that instead
> of writing
>  (posn-x (bird-posn (sky-bird (world-sky some-world))))
> you can write -- in Racket --
>  some-world.sky.bird.posn.x
> and make Racket programs look as short as C program selectors
> or Java dots or Javascript dots etc.
> The PRICE you pay is that you annotate variables and fields
> with struct information. That is, you have to say somewhere that
> some-world is a variable that always stands for a world struct.
> And the sky field in world has to come with information that it
> always is a sky-struct. And so on.

As describe in the HelpDesk about `prop:procedure' it is
also possible to use the struct id as a procedure, so that it can be
used as an accessor/mutator.

This is an intermediate solution, as it is nearly (but not quite) as
short as the dot notation without introducing global names.

Just a quick example that needs more error-checking and needs
to be abstracted (and, as Matthias and Shriram are pointing out,
this example has a f["baz"] flavor... I don't know if it is possible
to fix that if the procedure is not a macro) :

#lang racket

(struct foo (bar baz)
  #:property prop:procedure
    [(self attr)
     (case attr
       [(bar) (foo-bar self)]
       [(baz) (foo-baz self)]
       [else (error "foo: No field named" attr)])]
    [(self attr val)
     (case attr
       [(bar) (set-foo-bar! self val)]
       [(baz) (set-foo-baz! self val)]
       [else (error "foo: No field named" attr)])]))

(let ([f (foo 1 2)])
  (f 'baz 3)
  (list (f 'bar) (f 'baz)))

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20100818/588b7f6c/attachment.html>

Posted on the users mailing list.