5

I don't understand the output from:

Remove[g];
Options[g] = {"asd" -> 2};
g[a_, Optional[z_?Positive, 1], OptionsPattern[]] := {a, z, OptionValue["asd"]}

g[2, {}, "asd" -> 4]

{2, 1, 4}

The documentation of PatternTest says:

Any result for test[pval] other than True is taken to signify failure

but Positive[{}] =!= True holds, so it appears that the { } argument is interpreted as an option although the head is not Rule. I'm not sure how this should be fixed because I don't understand why it is wrong as it stands. What am I missing?

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
MeMyselfI
  • 1,116
  • 5
  • 12

1 Answers1

5

O.k. from the comments and the OptionsPattern documentation:

Any nesting of empty lists will match OptionsPattern

I understand now why the pattern matched against my expectation. To make it stop and behave the way I originally had in mind, the last argument (OptionsPattern[]) must be replace by

e : OptionsPattern[] /; MatchQ[Unevaluated[e], Repeated[_Rule | _RuleDelayed]]

After realizing that nested lists are intended as a valid way to specify options, however, it makes more sense to just keep things as in the OP.

MeMyselfI
  • 1,116
  • 5
  • 12