Re: Reviewing named and optional parameters Per Bothner 10 Jun 2021 17:23 UTC


On 6/9/21 2:00 AM, Daphne Preston-Kendal wrote:
> Could you give an example of how I can use [Kawa] to call apply using a quoted keyword object (or by extension a keyword object I might have lying around in a variable for example)? No matter what I try, I keep getting gnu.mapping.WrongArguments. (Kawa 3.1.1.)
>
> (define (f #!key k) k)
>
> None of these work:
> (apply f (arglist 'k: 1))
> (apply f (arglist @:(arglist 'k: 1)))
> (apply f (arglist @:(list 'k: 1)))
> or the same with @ or argvector instead of arglist.

Slight delay in replying because I had to fix some bugs - if you
want to try out the following, I'm afraid you have to build Kawa from gitlab.

The web-page mentions (briefly, without examples) that the
value after the "@:" can be a hash-table - or more generally a java.util.Map.
For example:

#|kawa:1|# (require 'hash-table)
#|kawa:2|# (define a (alist->hash-table (list (cons 'k: 12))))
#|kawa:3|# (define (f #!key k) k)
#|kawa:4|# (apply f a)
12
#|kawa:5|# (f @:a)
12
#|kawa:6|# (apply f (arglist @:a))
12

Kawa does't have a pretty syntax for hash-tables, alas, though it has one for vectors.

Note that an alternative to "a keyword object I might have lying around in a variable"
is using "a (keyword, value)-pair I might have lying around in a variable".  That
seems like to be more useful, and can be done without using a hash-table:

(define options (arglist k: 12))

--
	--Per Bothner
xxxxxx@bothner.com   http://per.bothner.com/