[racket] Macro defining functions question
On May 15, 2014, at 12:30 PM, Asumu Takikawa <asumu at ccs.neu.edu> wrote:
> On 2014-05-15 15:24:01 -0400, Matthias Felleisen wrote:
>>> Incidentally the macro stepper shows bar in the define differently
>>> colored from the rest of the definition, while it shows y in the baz
>>> definition as being differently colored. I’m not sure what the color
>>> key is though!
>>
>> Click on the identifiers to inspect their properties.
>
> BTW, you may need to adjust your macro stepper settings to see this.
>
> Stepper->View syntax properties.
>
> Cheers,
> Asumu
Thanks guys! That gets me a bit closer to wrapping up the diversion I undertook playing around with generics. There might be a better way to do this, but what I was interested in was a way of providing the generic binding for helper functions. So far I came up with this (although wouldn’t it be nice if there was just an “anaphoric-style it” we could make use of built into the dispatch?)
#lang racket
(require (for-syntax racket/syntax)
racket/generic)
(define-syntax (helper-bind-gen stx)
(syntax-case stx ()
[(_ g)
(with-syntax ([gen:name (format-id stx "~a" #'g)]
[parm:name (format-id stx "parm:~a" #'g)]
[bind:name (format-id stx "bind:~a" #'g)]
[with:name (format-id stx "~a" 'with-bind)])
#'(begin
(define parm:name (make-parameter #f))
(define-syntax bind:name
(syntax-id-rules ()
[(set! _ e) (set! parm:name (make-parameter e))]
[(_ args (... ...)) ((parm:name) args (... ...))]
[_ (parm:name)]))
(define-syntax (with:name stx)
(syntax-case stx ()
[(_ g (f args (... ...)))
(with-syntax ([parm:name (format-id stx "parm:~a" #'g)])
#'(parameterize ([parm:name g]) (f args (... ...))))]))))]))
(define-generics w
[set-x! w v])
(struct foo (x)
#:mutable
#:methods gen:w
[(helper-bind-gen w)
(define (helper v) (set-foo-x! bind:w v))
(define (set-x! w v) (with-bind w (helper v)))])
(define f1 (foo 3))
(set-x! f1 10)
(foo-x f1)
-Kevin