##Association
/<||>
objects are Atomic and thus unmatchable before 10.4
AtomQ@Association[]
yields True
.
This is confusing because it is not stated anywhere in the manual. For example tutorial/BasicObjects#15871 claims that only numbers (including complex ones), Strings and Symbols are atomic objects. guide/AtomicElementsOfExpressions does not mention Association
either, neither does guide/Associations.
Association@@{a -> b, c -> d}
does not act like association @@ {a -> b, c -> d}
, although the FullForm
suggests it does
association @@ {a -> b, c -> d} // FullForm
Association @@ {a -> b, c -> d} // FullForm
The Association[]
constructor function does a non trivial job, such that the following are both False
:
MatchQ[Association[], Unevaluated@Association[]]
MatchQ[Unevaluated@Association[], Association[]]
Also, MatchQ[<|x->1|>, Association[___]] === False
. Note: MatchQ[<|x->1|>, Association[___]]
should evaluate to True
(checked with version 11.1)
standard pattern matching inside the structure will not work.
You are probably best off converting associations to rule lists before pattern matching via Normal
: MatchQ[Normal@<|x -> 1|>, {___}] === True
.
Statement from WRI
It just so happens that Association is currently AtomQ, though I've argued strongly against that fact internally, and I've got SW's say-so that we'll change that eventually. But that doesn't have all that much to do with pattern matching not working inside associations: we all agree it should, it's just hard to do efficiently and we couldn't deliver it for 10. So, to sum up: Association will not be AtomQ forever, and it will support pattern matching eventually. There's a correlation here, not a causation.
Further reading
MatchQ-ing Associations (MMA 10)
Fixed in 10.4
In Mathematica 10.4,
Association
can now be used in pattern matching.
There's now also
KeyValuePattern
which is a pattern object specifically to match elements of an Association or list of rules.