[racket] Bouncing default value
On 2/11/2012 10:55 AM, Laurent wrote:
> Hi,
>
> Often I'd like to make a wrapper for a given function that has default
> values (possibly with keywords).
> So if I want my function to have the same default values for one
> particular optional argument, I need to redefine it myself.
>
> For example, suppose there already exists a function foo like this:
>
> (define (foo arg1 [arg2 <some-complicated-default-value>])
> ....)
>
> And I want to make a wrapper for foo on arg1, but keep foo's arg2
> default value:
>
> (define (bar [arg2 <the-same-complicated-default-value>])
> (foo 5 arg2))
>
> But I don't feel right when I need to redefine the default argument,
> for a few reasons:
> - I feel bar should not need to know foo's arg2 default value.
> - if arg2 has a complicated default argument, it is "painful" and
> error-prone to rewrite it (and if the docs for the default value for
> arg2 are simplified, it may be problematic),
> - if the specification of foo's arg2 changes, I need to change bar too
> (though sometimes bar's arg2 needs not reflect the change, but often
> it does),
>
> I would prefer something like:
>
> (define (bar [arg2 (get-default-value foo arg2)])
> (foo 5 arg2))
>
> which is transparent w.r.t. foo.
>
> Does this feature exist, or does it even have a name? Or can it be
> easily implemented (maybe keywords could help)?
>
> Thanks,
> Laurent
>
I simply use
(define (bar . x)
(apply foo 5 x))