Reputation: 4157
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. )
Upvotes: 8
Views: 826
Reputation: 22579
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).
Upvotes: 11
Reputation: 6520
This?
f[x_Integer] := 1
f[x_] := Message[errm::err, x]
f[] := Message[errm::err]
Upvotes: 10