Consider this function
w[x_Integer: 2, y__List : {1, 2}, z_String: "X"] := {x y, z}
w[4]
{{4, 8}, "X"}
Now I want to define an error message to be displayed in case of a wrong entry. The way I know is
w::argerr = "error with `1`, `2`, `3`";
w[x1___, x2___, x3___] := (Message[w::argerr, x1, x2, x3]; $Failed);
which only works well when all the arguments are provided. For example
w[x]
StringForm::sfr: Item 2 requested in "error with
1
,2
,3
" out of range; 1 items available.StringForm::sfr: Item 3 requested in "error with
1
,2
,3
" out of range; 1 items available.w::argerr: error with l,
2
,3
$Failed
How to construct a proper error message when one of the entry is wrong and others are supposed to take default values. For example the error message I am looking for
w[x]
error with x
w[4,2]
error with 2
w[x_: 2, y_: {1, 2}, z_: "X"] := If[! IntegerQ[x], Message[w::argerr, x, y, z]; $Failed, True]
displays the error message correctly using the default values for inputs likew[x]
, if that's what you were going for? $\endgroup$x
as first, second, and third argument, as seen in something likeReplaceList[{x}, {a_: 1, b_: 2, c_: 3} :> {a, b, c}]
. This would get really messy if you were to deal with second/third arguments. $\endgroup$