11
$\begingroup$

Mathematica's help on SyntaxInformation does not rule out syntax information for pure functions.

SyntaxInformation[f] gives information used to generate syntax coloring and other advisories when f[…] is entered as input.

The naive code

f = # &;
SyntaxInformation[f] = {"ArgumentsPattern" -> {_}}

fails with a message about the first argument. Using the string instead of the symbol

SyntaxInformation["f"] = {"ArgumentsPattern" -> {_}}

does not throw a message but it does not produce the expected red parameters in f[a,b].

Is there any way to specify syntax information on pure functions?

$\endgroup$
2
  • 2
    $\begingroup$ SyntaxInformation works at the Symbol level. Call it before assigning to f. $\endgroup$
    – b3m2a1
    Jan 17, 2018 at 20:11
  • 3
    $\begingroup$ Although the solution is quite simple, it is far from trivial. Write it as an answer, I will accept it. $\endgroup$
    – Hector
    Jan 17, 2018 at 20:15

1 Answer 1

12
$\begingroup$

SyntaxInformation does not hold its arguments so you can hold it:

f // ClearAll
f = # &;
f // Unevaluated // SyntaxInformation = {"ArgumentsPattern" -> {_}};

as an alternative to altering the order:

f // ClearAll
f // SyntaxInformation = {"ArgumentsPattern" -> {_}};
f = # &;
$\endgroup$

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.