Here's a modification to the code I posted below.  It lets the variable
transformers work with anything, not just nth.

@@ -8,7 +8,7 @@
       ((_ #(expr "string") n)
        (string-ref expr n))
       ((_ spec n)
-       (spec n))))
+       (spec nth n))))
 
   (define-syntax define-typed
     (lambda (stx)
@@ -21,8 +21,8 @@
               (let-syntax ((arg (make-variable-transformer
                                  (lambda (stx)
                                    (syntax-case stx (set!)
-                                     ((_ n)
-                                      (syntax (nth #(t type) n)))
+                                     ((_ m a (... ...))
+                                      (syntax (m #(t type) a (... ...))))
                                      (_ (identifier? stx)
                                       (syntax t))
                                      ((set! _ val)

-- 
: Derick
----------------------------------------------------------------


On Tue, 2009-09-29 at 15:51 -0700, Derick Eddington wrote:
> On Sun, 2009-09-27 at 16:59 -0500, Eduardo Cavazos wrote:
> > Now, it would be nice to be able to say:
> > 
> > (define-typed (abc #(seq "string"))
> >    (nth seq 2))
> 
> > But that has at least one problem.
> 
> > So, any suggestions for how to approach 'define-typed'?
> 
> Here's a way.  It requires coordination with the macros like nth.
> 
>   (define-syntax nth
>     (syntax-rules ()
>       ((_ #(expr "list") n)
>        (list-ref expr n))
>       ((_ #(expr "vector") n)
>        (vector-ref expr n))
>       ((_ #(expr "string") n)
>        (string-ref expr n))
>       ((_ spec n)
>        (spec n))))
> 
>   (define-syntax define-typed
>     (lambda (stx)
>       (syntax-case stx ()
>         ((_ (name #(arg type) ...) . body)
>          (with-syntax
>              (((t ...) (generate-temporaries (syntax (arg ...)))))
>            (syntax
>             (define (name t ...)
>               (let-syntax ((arg (make-variable-transformer
>                                  (lambda (stx)
>                                    (syntax-case stx (set!)
>                                      ((_ n)
>                                       (syntax (nth #(t type) n)))
>                                      (_ (identifier? stx)
>                                       (syntax t))
>                                      ((set! _ val)
>                                       (syntax (set! t val)))))))
>                            ...)
>                 . body))))))))
> 
> 
> (define-typed (foo #(s "string") #(l "list"))
>   (values (nth s 2) (nth l 3)))
> 
> (foo "abc" (quote (a b c d)))
> =>
> #\c
> d
> 
> 


Reply via email to