Skip to content

Commit

Permalink
make D[x,y...] correspond to D[(x,y...)] for a dictionary D; see Juli…
Browse files Browse the repository at this point in the history
  • Loading branch information
stevengj committed Nov 20, 2013
1 parent e4361f6 commit 2eea897
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
5 changes: 5 additions & 0 deletions base/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ function getindex(t::Associative, key)
return v
end

# t[k1,k2,ks...] is syntactic sugar for t[(k1,k2,ks...)]. (Note
# that we need to avoid dispatch loops if setindex!(t,v,k) is not defined.)
getindex(t::Associative, k1, k2, ks...) = getindex(t, tuple(k1,k2,ks...))
setindex!(t::Associative, v, k1, k2, ks...) = setindex!(t, v, tuple(k1,k2,ks...))

push!(t::Associative, key, v) = setindex!(t, v, key)

# hashing objects by identity
Expand Down
2 changes: 2 additions & 0 deletions doc/stdlib/base.rst
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,8 @@ Dicts can be created using a literal syntax: ``{"A"=>1, "B"=>2}``. Use of curly
As with arrays, ``Dicts`` may be created with comprehensions. For example,
``{i => f(i) for i = 1:10}``.

Given a dictionary ``D``, the syntax ``D[x]`` returns the value of key ``x`` (if it exists) or throws an error, and ``D[x] = y`` stores the key-value pair ``x => y`` in ``D`` (replacing any existing value for the key ``x``). Multiple arguments to ``D[...]`` are converted to tuples; for example, the syntax ``D[x,y]`` is equivalent to ``D[(x,y)]``, i.e. it refers to the value keyed by the tuple ``(x,y)``.

.. function:: Dict()

``Dict{K,V}()`` constructs a hashtable with keys of type K and values of type V.
Expand Down
4 changes: 4 additions & 0 deletions test/collections.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ for i=10000:20000
end
h = {"a" => 3}
@test h["a"] == 3
h["a","b"] = 4
@test h["a","b"] == h[("a","b")] == 4
h["a","b","c"] = 4
@test h["a","b","c"] == h[("a","b","c")] == 4

let
z = Dict()
Expand Down

0 comments on commit 2eea897

Please sign in to comment.