Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mathematica: How to check if no argument is supplied to a function?

How to check if no argument is supplied to a function?

For example, if I have:

  f[x_Integer]:=1
  f[x_]:=Message[errm::err, x]

and I call f with no argument:

   f[]

'nothing happens', I want to force a specific (error-)condition.

( Background: I am making MUnit tests for packages and OO-System classes. )

like image 525
nilo de roock Avatar asked Jul 02 '11 19:07

nilo de roock


2 Answers

As an alternative to explicitly listing the zero-args possibility, you can do

f[x_Integer] := 1
f[args___] := (Message[errm::err, {args}];$Failed);

which would also catch the error cases of several passed arguments (assuming that it is an error).

like image 133
Leonid Shifrin Avatar answered Oct 13 '22 15:10

Leonid Shifrin


This?

f[x_Integer] := 1
f[x_] := Message[errm::err, x]
f[] := Message[errm::err]
like image 28
acl Avatar answered Oct 13 '22 16:10

acl