[racket] Bouncing default value

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Sat Feb 11 12:44:50 EST 2012

Would this help you, suitable wrapped up as a macro: 

#lang racket 

(struct f (g default) #:property prop:procedure (struct-field-index g))

(define f* (f (lambda (x [y ((f-default f*))]) (+ x y)) (lambda () 99)))

(define (f-wrapper f-ish)
  (lambda (g [d ((f-default f-ish))]) ;; all you need to know here is that there is a default argument and it is computed from a thunk 
    (displayln `(wrapped ,g))
    (f-ish g d)))

(define f-wrapped (f-wrapper f*))

On Feb 11, 2012, at 11: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
> ____________________
>  Racket Users list:
>  http://lists.racket-lang.org/users

Posted on the users mailing list.