Re: SyntaxInformation ArgumentsPattern with overloaded functions
- To: mathgroup at smc.vnet.net
- Subject: [mg105828] Re: SyntaxInformation ArgumentsPattern with overloaded functions
- From: magma <maderri2 at gmail.com>
- Date: Sun, 20 Dec 2009 06:57:06 -0500 (EST)
- References: <hgd7rq$blr$1@smc.vnet.net>
On Dec 17, 1:23 pm, PeterDickof <peter.dic... at saskcancer.ca> wrote: > Define function... > f[x_]:=x^2 > ..then... > SyntaxInformation[f]={"ArgumentsPattern"->{_}} > ..specifies that it should have 1 argument > > If, instead, ... > f[x_,y_,z_]:=x^2+(y/z)^2 > .. then ... > SyntaxInformation[f]={"ArgumentsPattern"->{_,_,_}} > specifies that it should have 3 arguments. > > If I have BOTH functions, how do I specify that I should have either 1 or= 3 arguments? This fails.... > SyntaxInformation[f]={"ArgumentsPattern"->{_}|{_,_,_}}; SyntaxInformation is one of those functions that are not very well documented. For example there is no example of the use of "ColorEqualSigns". Can somebody shed some light on it? The only way to obtain what you want (which is very reasonable), is IMO the following: SyntaxInformation[func] = {"ArgumentsPattern" -> {_, _, _.}} Note that the last pattern is an optional one (it has a dot). So when you are typing your input Mathematica will not complain after the second argument - if you keep typing the third argument - and she won't complain either if you don't. That's because it is an optional argument (very smart uh :-)). However if you try to type a fourth argument, Mathematica will issue a syntax error colouring. After all: you are allowed only 1 option, not more. So a full example would be: SyntaxInformation[func] = {"ArgumentsPattern" -> {_, _, _.}}; func[x_, y_] := {x, y}; func[x_, y_, z_] := {x, y, z}; Now start typing: func[] first, and see the red missing-argument-caret. Then start filling the square brackets with arguments and see how Mathematica responds. Example func[1,2,...] When Mathematica feels you are doing something wrong she will colour the argument in red. As you can see she behaves well with 2 or 3 arguments, but not with 4 or more. Other cases. 1- If you wanted a function with 2 or more arguments: SyntaxInformation[func] = {"ArgumentsPattern" -> {_, _, ___}}; where the last pattern is a triple blank. Or perhaps equivalently: SyntaxInformation[func] = {"ArgumentsPattern" -> {_, __}}; where the last pattern is a double blank. 2- If you wanted a function with 2 or 3 or 4 arguments: SyntaxInformation[func] = {"ArgumentsPattern" -> {_, _, _.,_.}}; where the last 2 patterns are optional type.