0
$\begingroup$

Consider the following matching:

p = Rule[_String, _String];
MatchQ[f[Rule["a", "b"], Rule["c", "d"]], f[p ..]]
(* True *)

If you replace f with List (or even outlandish operators like Grid or SingularValueDecomposition), it still matches:

MatchQ[List[Rule["a", "b"], Rule["c", "d"]], List[p ..]]
(* True *)

However, if you replace f with Association, it doesn't match:

MatchQ[Association[Rule["a", "b"], Rule["c", "d"]], Association[p ..]]
(* False *)

Why is this? Ordinarily I would suspect that evaluation changes the form, but calling FullForm[Association[Rule["a", "b"], Rule["c", "d"]]] returns Association[Rule["a", "b"], Rule["c", "d"]], ie, its form is unchanged.

Ultimately I'd like a pattern that matches Associations of Strings to TemporalDatas, and while trying to figure out why my pattern didn't work, I found the above minimal(ish) example.

$\endgroup$
  • 2
    $\begingroup$ Association is a special construct. Use KeyValuePattern instead. And look up a bunch of WReach’s answers about Association. $\endgroup$ – b3m2a1 Jan 2 '18 at 0:45
  • $\begingroup$ Looks like KeyValuePattern was introduced in V10.4, but I'm on V10.2. ARGH $\endgroup$ – Peter Richter Jan 2 '18 at 0:47
  • $\begingroup$ Then the best thing to do is to use _Association?someValidationFunction I think $\endgroup$ – b3m2a1 Jan 2 '18 at 0:50

Browse other questions tagged or ask your own question.