idiom for ignoring other keys

96 views
Skip to first unread message

Tamas Papp

unread,
Feb 21, 2015, 3:23:24 AM2/21/15
to juli...@googlegroups.com
Hi,

I am looking for something similar to &ALLOW-OTHER-KEYS in Common Lisp,
so that a function would only use keywords that it needs and ignore the
rest, eg how to write `f`

f(; a=1, b=2) = (a,b)

so that the following does not result in an error:

f(; Dict(:a => 5, :b => 7, :c => 9)...)

I found the following workaround:

function f(; args...)
args = Dict(args)
(args[:a], args[:b])
end

Is there anything simpler (or is it planned)?

Best,

Tamas

Milan Bouchet-Valat

unread,
Feb 21, 2015, 4:58:17 AM2/21/15
to juli...@googlegroups.com
I would simply write it this way:
f(; a=1, b=2, unused_args...) = (a, b)

But of course this function is misleading as-is, since it does not
complain if you pass unused arguments. A more common/useful situation
would be something like:
g(; a=1, b=2, h_args...) = (a, b, h(; h_args...))
h(; c=3) = 2 * c



Regards

Tamas Papp

unread,
Feb 21, 2015, 11:09:28 AM2/21/15
to juli...@googlegroups.com
Thanks. I have a related question: given a function like

f(; a=1, b=2) = (a,b)

is there a way to call it with keyword arguments which are a superset of
a,b, so that the extras are ignored?

This is implemented by :ALLOW-OTHER-KEYS in CL, eg

(defun f (&key (a 1) (b 2)) (list a b))

(f :allow-other-keys t :a 1 :b 2 :c 'ignored)

Best,

Tamas
Reply all
Reply to author
Forward
0 new messages