Say we have a pattern defined as:
pattern = {foo[OptionsPattern[]] ...};
We can then use MatchQ
to see whether an expression matches pattern
, and some simple results work as I expect:
In[2]:= MatchQ[{foo[a -> b]}, pattern]
Out[2]= True
In[3]:= MatchQ[{foo[]}, pattern]
Out[3]= True
In[4]:= MatchQ[{foo[a -> b], foo[a -> b]}, pattern]
Out[4]= True
However, other things don't work as I'd expect:
In[5]:= MatchQ[{foo[a -> b], foo[]}, pattern]
Out[5]= False
In[6]:= MatchQ[{foo[a -> b], foo[a -> c]}, pattern]
Out[6]= False
As far as I can tell, different occurrences of OptionsPattern[]
all have to match the same thing for the pattern as a whole to match, much like named patterns, where {x_, x_}
will match {a, a}
, but not {a, b}
. However, there doesn't seem to be anything about this in the documentation, and it's hard to be certain that's what's going on.
FWIW, there's an easy workaround, where you use
pattern = {foo[___?OptionQ]...}