Understood. However, ordered dictionaries would have the same relationship to vectors as namedtuples have to tuples, which is good for making the user interface for this syntax sugar consistent. Also, ordered dictionaries seem to be generally a better fit for Julia’s target audience (basically the same as Python’s target audience).
One suggestion that has been made is that ordered dictionaries can be indexed in order by a special ordinal index type (e.g. my_dict[6th]
); any other non-token index will be treated as a hashkey.
I’m not finding it, so I’ll spitball some thoughts here:
To initialize:
[;] # OrderedDict{Any,Any}()
[a=1, b=2] # OrderedDict{Symbol, Int}([:a,:b], [1,2])
[; a=1, b=2] # alternative syntax
['a'=1, 'b'=2] # OrderedDict{Char,Int}(['a','b'], [1,2])
[a=1, 'b'=2] # OrderedDict{Any,Int}([:a, 'b'], [1,2])
# note that the l.h.s. can be any literal
# multi-dimensional: (???)
[
(1,1) = 1 (1,2) = 0
(2,1) = 0 (2,2) = 2
]
# generator:
[; [Symbol("key",i)] = i^2 for i=1:5]
# non-literal l.h.s.:
[[x]=1, [y]=2] # access variables x and y as keys
[[:a]=1, [:b]=2] # another alternative for [a=1, b=2]
To access:
d = [a=1, 0=2] # OrderedDict{Any,Int}([:a,0], [1,2])
d[:a] == d.a == d[1th] == 1
dt = Tokenizer(d) # generates tokens from keys
d[dt(:a)] == d.a
d[0] == d[dt(0)] == d[2th] == 2
let t = dt(some_key, 0) # 2nd argument sets default value if key not found?
d[t] += 1
end
d.:0 = 3 / d.:0
(since getproperty
and setproperty!
on dictionaries is otherwise pretty useless, I see no reason not to act like a PropDict
. This also builds consistency with NamedTuple
.)