Defining UpValues
- To: mathgroup at smc.vnet.net
- Subject: [mg110777] Defining UpValues
- From: István Zachar <zac at freemail.hu>
- Date: Mon, 5 Jul 2010 21:14:40 -0400 (EDT)
Dear Group,
consider the following code:
func[sys_Integer] := sys;
func /: func[sys_Integer][args___] := sys + 1;
func[sys_Function] := sys;
{
func[1],
func[1][2],
func[# + 2 &],
func[# + 2 &][2]
}
Is there a way to define func such a way that when the main argument
is an Integer, any further arguments are ignored? That is, to
return: {1, 2, #1 + 2 &, 4} instead of {1, 1[2], #1 + 2 &,
4}. At present, the second function definition is basically ignored by
the kernel.
I am aware that func[sys_type, args___] would be the easiest way to
use here, but in my real code
1. there is a large number of named options supplied with sys
preventing (or at least making hard) the use of *any number* of
optional second arguments;
2. sometimes the main call (func[sys]) is separated from the
introduction of any optional argument (this is part of a GUI), which
may cause calls like this:
x = func[some_function]; ... (* calculations involving x *) ... ;
y = x[2];
Thanks in advance,
Istv=E1n