开发者

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:开发者_StackOverflow社区

   f[]

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

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


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).


This?

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜