[plt-scheme] with-struct
On Wed, Oct 21, 2009 at 02:50:11PM -0400, Eli Barzilay wrote:
> On Oct 21, Jon Rafkind wrote:
> > Here is a macro that introduces fields of a struct into the current
> > lexical environment so that instead of saying (foo-x some-foo) you
> > can just say 'x', similar to pascal's `with' construct.
> >
> > I had to resort to string munging to get the original field
> > names. Is there a better way? I guess the "right" answer is to make
> > the user pass in the field names themselves but I dislike such
> > verbosity.
>
> The right answer, IMO, and if this was desirable, is to make the
> struct information include the field names, which would probably not
> even be too much work.
>
> But I don't think that this is a good idea. If anything, then I think
> that an extension to `define-struct' works out better. For example
> (using a horrible name):
>
> (define-struct foo (n m b) #:mutable #:with-scoper)
> (let ([my-foo (make-foo 1 2 3)])
> (with-foo my-foo
> (set! n 9)
> (printf "n is ~a\n" n)))
>
> And BTW, that's one of the first fights I had with the then-new module
> system and Swindle -- I used to have a `with-slots' macro that did
> just the above dynamically, and it was a bad idea for obvious reasons.
Getting the names from the type of the argument might be feasible in
typed scheme.
-- hendrik