Bug introduced in 10.4 and persisting through 11.1.1
Up until 10.4 you could not match on internal Association
structure at all: https://mathematica.stackexchange.com/a/119542/6804
Now you can, but it seems flawed still: the following patterns should match <|"c" -> {0}|>
but not <|"c" -> {1}|>
. These work:
gok1[KeyValuePattern@{"c" -> c_List /; First@c == 0}] := 0;
gok2[KeyValuePattern@{"c" -> c_List}] := 0 /; First@c == 0;
(I still don't understand why they support the second variant, after all the RHS in := should not be a Pattern...).
But this one does not:
gfail[KeyValuePattern@{"c" -> c_List}] /; First@c == 0 := 0;
giving First::normal: Nonatomic expression expected at position 1 in First[c].
but only the first time it is used, e.g. via
gfail[<|"c" -> {0}|>]
gfail[<|"c" -> {1}|>]
or
gfail[{"c" -> {0}}]
gfail[{"c" -> {1}}]
So the culprit seems to be the KeyValuePattern
.
If it is not involved, everything works fine:
hok[{"c" -> c_List}] /; First@c == 0 := 0;
hok1[{"c" -> c_List /; First@c == 0}] := 0;
hok2[{"c" -> c_List}] := 0 /; First@c == 0;
hok[{"c" -> {0}}]
hok[{"c" -> {1}}]
hok1[{"c" -> {0}}]
hok1[{"c" -> {1}}]
hok2[{"c" -> {0}}]
hok2[{"c" -> {1}}]
Am I missing something or can somebody confirm this as a probable bug?
patt /; test
c
isn't shared but forlhs:>rhs/;test
it is:a : KeyValuePattern[{"c" -> c_List}] :> a /; (First[c] === 0)
. Just an observation, don't know if the observerd behavior is to be expected (isn't for me). $\endgroup$