5
$\begingroup$

I wonder if the following behaviour of SyntaxInformation is a bug: I want a function f to accept one argument and options, so I use

Options[f] = {foo->1};
SyntaxInformation[f] = {"ArgumentsPattern"->{_,OptionsPattern[]}};

The output of

{f[], f[a], f[a, foo -> 1], f[a, bar -> 2], f[a, b], f[a, b, foo -> 3], f[a, b, bar -> 4]}

is displayed asWrong highlighting

The first four results are as I expect them to be, but the second argument b should also be coloured red. Am I missing something or is this a bug of SyntaxInformation?

$\endgroup$
0

1 Answer 1

5
$\begingroup$

I noticed this too, but didn't really think very hard about why it is like this. One likely reason is that you can store an option in a variable.

opt = foo -> 1
f[x, opt]

This is not just a theoretical scenario, it is something I actually use often, though in practice I usually have multiple options:

opts = {foo -> 1, bar -> 2}
f[x, Sequence @@ opts]

Or alternatively:

opts = Sequence[foo -> 1, bar -> 2]
f[x, opts]

Some builtin functions (though not all) accept option lists as well. This is valid and it work without Sequence:

optList = {AspectRatio -> 1, PlotRange -> All}
Graphics[..., optList]

Of course one can do the very same thing with non-option arguments as well, but the key here is that it is fairly common to see this pattern with options, while it's unusual with general arguments.

So my take on this: not a bug, but desirable behaviour.

$\endgroup$
6
  • $\begingroup$ Since MatchQ[ asd, OptionsPattern[] ] === False I wouldn't say it is desirable, or maybe it is sometimes but it should not be done automatically. It's one of those cases where MMA wants to force people to be happy. $\endgroup$
    – Kuba
    Dec 16, 2015 at 10:35
  • $\begingroup$ @Kuba The "patterns" in SyntaxInformation have little to do with the usual Mathematica patterns. They are interpreted by the front end, not the kernel, and should just be treated as a convenient and intuitive notation for specifying allowed arguments. They could have use a completely different syntax for this, but would that be really better? $\endgroup$
    – Szabolcs
    Dec 16, 2015 at 10:38
  • $\begingroup$ I know that, but without proper explanation, it is really bad. There is nothing about not considering those patterns as valid patterns. $\endgroup$
    – Kuba
    Dec 16, 2015 at 10:43
  • $\begingroup$ @Kuba I think it's pretty clear from the documentation that these are not really standard patterns. This is not the only example of such behaviour. E.g. the second argument in {_, {_, _}} will not highlight something that is not a list (but it will highlight a list of the wrong length). The more I think about it the more I believe that without these choices SyntaxInformation couldn't be particularly useful. This is about "making people happy" only in the sense that having an actually usable tool is what people want. $\endgroup$
    – Szabolcs
    Dec 16, 2015 at 10:49
  • $\begingroup$ I agree it is handy, you also convinced me that the fact that one should not consider them as valid patters is easy to get. But I still think that it is not expected when the name remains OptionsPattern. $\endgroup$
    – Kuba
    Dec 16, 2015 at 11:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.