guix-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH 2/2] utils: Support defaults in substitute-keyword-arguments.


From: Ludovic Courtès
Subject: Re: [PATCH 2/2] utils: Support defaults in substitute-keyword-arguments.
Date: Fri, 30 Sep 2016 22:24:22 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (gnu/linux)

Eric Bavier <address@hidden> skribis:

> On Sat, 24 Sep 2016 11:21:40 +0900
> address@hidden (Ludovic Courtès) wrote:
>> 
>> > +replaced by EXP.  EXP is evaluated in a context where VAR is bound to the
>> > +previous value of the keyword argument, or DFLT if given."
>> > +    (syntax-case x ()
>> > +      ((_ original-args ((kw var dflt ...) exp) ...)
>> > +       #`(let loop ((args (default-keyword-arguments
>> > +                            original-args
>> > +                            (list #,@(append-map (match-lambda
>> > +                                                   ((k) '())
>> > +                                                   (x x))
>> > +                                                 #'((kw dflt ...) ...)))))
>> > +                    (before '()))  
>> 
>> I would prefer to stick to ‘syntax-rules’ when matching the clauses,
>> with a helper macro:

[...]

> From 9481121fef60f0c3f4ea0f742d77336906771167 Mon Sep 17 00:00:00 2001
> From: Eric Bavier <address@hidden>
> Date: Tue, 20 Sep 2016 15:41:31 -0500
> Subject: [PATCH] utils: Support defaults in substitute-keyword-arguments.
>
> * guix/utils.scm (collect-default-args, expand-default-args): New
> syntax.
> (substitute-keyword-arguments): Allow default value declarations.
> * tests/utils.scm (substitute-keyword-arguments): New test.

[...]

> +(define-syntax collect-default-args
> +  (syntax-rules ()
> +    ((_ (_ _))
> +     (list))
> +    ((_ (kw _ dflt))
> +     (list kw dflt))
> +    ((_ (_ _) rest ...)
> +     (collect-default-args rest ...))
> +    ((_ (kw _ dflt) rest ...)
> +     (cons* kw dflt (collect-default-args rest ...)))))

I think you just need the base case (zero args), the default-value case,
and the no-default-value case:

  (define-syntax collect-default-args
    (syntax-rules ()
      ((_)   ;done
       '())
      ((_ (kw var) rest ...)
       (collect-default-args rest ...))
      ((_ (kw var dflt) rest ...)
       (cons* kw dflt (collect-default-args rest ...)))))

> +(define-syntax expand-default-args
> +  (syntax-rules ()
> +    ((_ original-args (kw var) ...)
> +     original-args)
> +    ((_ original-args clause ...)
> +     (default-keyword-arguments
> +       original-args
> +       (collect-default-args clause ...)))))

AFAICS this macro can be removed altogether.

OK with changes along these lines, thanks!

Ludo’.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]