MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Defining UpValues

Dear Group,

consider the following code:

func[sys_Integer] := sys;
func /: func[sys_Integer][args___] := sys + 1;
func[sys_Function] := sys;

 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,


  • Prev by Date: Copying and Pasting from a Grid with Dividers
  • Next by Date: Re: a 4d algebraic geometry problem
  • Previous by thread: Re: Copying and Pasting from a Grid with Dividers
  • Next by thread: Re: Defining UpValues