[patch] set setter for array-ref to array-set!
Per Bothner
per@bothner.com
Fri Mar 6 19:15:00 GMT 2015
On 03/04/2015 11:54 PM, Jamison Hope wrote:
> What do you think about doing similar things for hash tables?
>
> This works (even though RNRS hashtable-ref takes a third argument):
>
> (import (rnrs hashtables))
> (set! (setter hashtable-ref) hashtable-set!)
>
> (define h (make-eq-hashtable))
> (set! (hashtable-ref h 'year) 2015)
> h => {year=2015}
It's a little ugly because of the weirdness with the default parameter -
i.e. that (hashtable-ref h 'year) is not a valid call, even after the set!.
Plus it doesn't make the code any more compact or readable.
> It seems like function call notation would be nice for java.util.Map
> instances, too:
>
> (set! (h k) v) => (h:put k v)
> (h k) => (h:get k)
>
> Thoughts? Part of a GSoC project, perhaps?
Too small for a GSoC project.
Using function call notation seems reasonable, but I think I had some concerns
I can't quite remember. One minor issue is the "conflict" with colon notation
when it comes to string or symbol keys. Should we prefer:
(my-table "foo")
or:
my-table:foo
That issue is probably a distraction.
Another issue to note: If we support function call notation, I'd like
it to work for any java.util.Map, not just those returned by make-hash-table
or make-hashtable.
Finally there is the issue of defaults. If a key isn't in my-table what should
be the result of this?
(my-table key)
Arguably this should throw an exception as key isn't in its domain.
The other alternative is to return #!null, for simplicity and compatibility
with the Java Map interface.
My inclination would be to throw an exception, and also introduce:
(my-table key default: default-value)
If default-value is #!null this should be compiled to a call to the Map get routine.
If default-value is non#!null it should be compiled to the getOrDefault method
if either the target is Java 8 or if my-table is known to be a sub-class of
AbstractHashTable; otherwise we call a static helper method.
(Of course if my-table isn't known to be a Map at compile-time we have to check
if it's a Map at run-time.)
Comments?
--
--Per Bothner
per@bothner.com http://per.bothner.com/
More information about the Kawa
mailing list