Syntax coloring for the following function definition
foo[x_List] := ...
will color green (and italicize by default) the argument name x
as well as the pattern name _List
:
On the other hand, syntax coloring for
foo[x:_List]
colors only the identifier x
green and not the _List
qualifier:
Is there a way to configure the front end to color just the identifier name as in the second example?
Looking through the Option Inspector, I cannot find any relevant switches, so I suspect not. I do wonder however if this behavior is a bug or a feature whose purpose I don't fully understand.
EDIT: The remaining issue in this question is the following: The syntax coloring rule which specifies that function argument names should be colored green in the body of the function in which these names are bound also colors green any head to the right of the underscore that is present for pattern matching purposes. In what sense is the underscore itself and any head that is attached to it part of the function argument name proper?
The two examples provided above exhibit a pair of semantically equivalent function definitions where the coloring (which is syntax) is applied differently to semantically equivalent parts of the respective function definitions.
EDIT: Here is another example where the syntax coloring rule for pattern names is being applied in a seemingly inconsistent fashion:
f[a + b] + f[a + c] /. f[a + x_] + f[a + y_] -> p[x, y]
Shouldn't the x
and the y
arguments in p[x,y]
be colored green much like in a function definition (whose syntax also defines a transformation rule)?
Another puzzling example:
MatchQ[{1, 1}, {x_?IntegerQ, x_ /; x > 0}]
In my front-end (v8) the first x
is black. On the other hand, the second and third instances are colored green.
These examples raise the issue of whether or not color syntax rules for pattern names as they are currently implemented cause more confusion than they are worth for helping the user visualize lexical scopes that are in force at a particular position in the code. Anyone else agree?
x_
is naked, whilex_
andx
is a pairing as the highlighting indicates. I am sorry that Mathematica does not have (AFAIK) more customizable highlighting to suit your wishes. It also bothers me that highlighting is not provided for{x} ~Block~ Mod[x, 2]
ori ~Table~ {i, 5}
but unlike you I find the existing highlighting internally consistent enough that I am used to it. $\endgroup$p[x,y]
is still a bit puzzling however. I'm glad that you find the syntax coloring rule for pattern names "consistent enough" for your own use. It would be helpful if someone could state in plain English here what the rule is so that others such as myself are not so "surprised" by the resulting application of the rule in examples like those cited above. $\endgroup$x
andy
inp[x,y]
should not be colored green because they are not localized; if you useRuleDelayed
(:>
) instead you will see that they are colored, because with that they are localized. A useful distinction. $\endgroup$:>
when using named patterns on the RHS to avoid this ambiguity, though conceivably there are certain uses when this ambiguity is useful. $\endgroup$