Laurent Orseau
There may be incompatibility with code that uses the keywords #:! and #:?.
a shortcut definition for keyword arguments to avoid the ubiquitous #:some-argsome-arg repetition,
a pass-through mechanism for optional keyword arguments to propagate default values without having to know them.
(make-listnumberfruit))> (make-fruits'apple#:number4);Notice the keyword name'(appleappleappleapple)
(make-listnumberfruit))> (make-fruits2'pear)'(pearpearpear)
> (make-fruits2'pear#:number4)'(pearpearpearpear)
;Let's write a function that uses `make-fruits2` without changing;the default value for `number`—whatever value this is.(list(make-fruits2fruit1#:numbernumber)(make-fruits2fruit2#:numbernumber)))> (make-two-fruits'apple'banana)'((appleappleapple)(bananabananabanana))
> (make-two-fruits'apple'banana#:number2)'((appleapple)(bananabanana))
(make-lista-numberfruit))(make-fruits3fruit#:numbernumber))> (make-fruits4'clementine)'(clementineclementineclementine)
syntax
( lambda argsbody...+)
syntax
( λ argsbody...+)
args = (pos-id...[opt-idopt-expr]...kw-arg...)| (pos-id...[opt-idopt-expr]...kw-arg.... rest-id)| rest-idkw-arg = #:!id| #:?id| #:?[idexpr]| keywordid| keyword[idexpr]
An argument of the form #:!name is equivalent to #:namename. An argument of the form #:?[nameval] is equivalent to #:name[nameval] but binds name to val only if name is no-value . An argument of the form #:?name is equivalent to #:name[nameno-value ].
This means in particular that (lambda (#:athe-a#:!a)...) is a syntax error (duplicate argument keyword), as well as (lambda (#:athe-a#:!the-a)...) (duplicate argument identifier).
Writing wrapper functions is already simplified with the new define thanks to pass-through optional arguments, but there can still be some verbosity left due to having to repeat the argument names. define-wrapper helps with this by passing the arguments to the wrapped function automatically.
syntax
keyword-arg...)maybe-call-wrappedbody...)maybe-call-wrapped =| #:call-wrappedcall-wrapped-id
The resulting function fun takes as input all the arguments arg...keyword-arg...maybe-rest. Only the arguments arg...maybe-rest are forwarded to the call to wrapped-fun. The function wrapped-fun must be defined elsewhere.
If call-wrapped-id is not provided then wrapped-fun is called in tail-position; otherwise it should be called as (call-wrapped-id) somewhere in body..., and this calls wrapped-fun with the arguments arg...maybe-rest.
(define-wrapper (foo(bara#:?[b'b])))
(bara#:bb))
#:cc)(set!a(+ac)))
(set!a(+ac))(bara#:bb))
#:cc)#:call-wrappedbar-wrapped(set!a(+ac))(displaylnres)res)
> (my-sort'(142)<)sort: contract violation
expected: (any/c . -> . any/c)
given: 'no-value
> (my-sort2'(142)<)'(124)
In standard Racket, when a function is defined with define , and is later called with the wrong number of arguments or with the wrong keywords, an error is signalled only at run time, when the function is called.
Instead, using the define form provided by define2 signals an error at compile-time.
Thus, such errors are caught much earlier than with standard Racket, for example with raco make. Furthermore, since DrRacket runs background expansion, such errors can be caught as early as the are written.
#true)> (foo)eval:17:0: foo: missing mandatory positional arguments
header: (foo bar (baz) #:fizz (#:buzz))
at: (foo)
in: (foo)
> (foo'a'b'c#:fizz'f)eval:18:0: foo: too many positional arguments
header: (foo bar (baz) #:fizz (#:buzz))
at: (foo 'a 'b 'c #:fizz 'f)
in: (foo 'a 'b 'c #:fizz 'f)
> (foo'a)eval:19:0: foo: missing keywords
header: (foo bar (baz) #:fizz (#:buzz))
at: (foo 'a)
in: (foo 'a)
> (foo'a#:fizz'f#:beurre'b)eval:20:0: foo: unknown keyword
header: (foo bar (baz) #:fizz (#:buzz))
at: #:beurre
in: (foo 'a #:fizz 'f #:beurre 'b)
For the last error, DrRacket even highlights the wrong keyword.
Thanks to Ross Angle, Sorawee Porncharoenwase, Jack Firth, Jens-Axel Soegaard, Sam Tobin-Hochstadt, Greg Hendershott, Bogdan Popa, Matthew Flatt, Robby Findler, and Leif Anderson for their help.