Co-optional arguments?
Racket and many other languages have the concept of optional arguments at the receiver side:
(define (a-procedure mandatory-arg #:foo [foo 123])
;; Use mandatory-arg and foo as normal
...)
(a-procedure mandatory-value #:foo 234) ;; override foo
(a-procedure mandatory-value) ;; leave foo alone
Here, the procedure is offering its caller the option of supplying a
value for foo, and if the caller does not do so, uses 123 as the
value for foo. The caller is permitted not to care (or even to
know!) about the #:foo option.
Almost no language that I know of has the symmetric case: an optional argument at the caller side (and because the feature doesn't exist, I'm having to invent syntax to show what I mean):
(define (run-callback callback)
(callback mandatory-value [#:foo 234]))
(run-callback (lambda (mandatory-arg) ...)) ;; ignores the extra value
(run-callback (lambda (mandatory-arg #:foo [foo 123]) ...)) ;; uses it
The intent here is that the procedure is permitted not to care (or even to know) about the additional value its caller supplies.
This would be useful for callbacks and other situations where the code invoking a procedure has potentially-useful information that the specific (and statically-unknown) procedure being called may or may not care about.
I said above that almost no languages have this feature: it turns
out that Squeak and
Pharo have something similar in their
BlockClosure>>#cull: methods. The following code works when given a
closure accepting zero, one or two arguments:
someBlock cull: 1 cull: 2 cull: 3
(Update: @msimoni points
out that
Common Lisp's
allow-other-keys
is similar, too. It still requires the callee to declare something
extra, though.)
