I am trying to improve my own dsolver API. Currently it takes optional arguments. Is it possible to specify that an optional argument should say be String only? Or List Only? Or Integer only? and so on.
Just like one does for normal arguments by using _Head
An example will make this more clear. Here is just an example, where the dsolve takes 2 optional arguments. The first is "ic" and the second is "hint". I want the value of the "hint" to be String.
Of course I can always check for this when I read the optional argument. But it will be better if Mathematica did that.
ClearAll[dsolve, y, x];
Options[dsolve] = {"ic" -> {}, "hint" -> ""};
dsolve[ode_Equal, y_[x_], x_, opts : OptionsPattern[{dsolve}]] := Module[{ODE, ic, hint},
ic = OptionValue["ic"];
hint = OptionValue["hint"];
ODE = <|"ode" -> ode, "y" -> y, "x" -> x, "ic" -> ic, "hint" -> hint|>;
ODE
]
Now I can call it using
dsolve[y''[x] + y[x] == 1, y[x], x, "hint" -> "linear"]
But I am also able to do this
dsolve[y''[x] + y[x] == 1, y[x], x, "hint" -> 3]
To fix this, I change the function to the following
dsolve[ode_Equal, y_[x_], x_, opts : OptionsPattern[{dsolve}]] := Module[{ODE, ic, hint},
ic = OptionValue["ic"];
hint = OptionValue["hint"];
If[Not[StringQ[hint]], Abort[]]; (*Added explicit check*)
ODE = <|"ode" -> ode, "y" -> y, "x" -> x, "ic" -> ic, "hint" -> hint|>;
ODE
]
and now
dsolve[y''[x] + y[x] == 1, y[x], x, "hint" -> 3]
Is there something like the following for making the optional arguments (I am making this up ofcourse, as this is wrong syntax, but just to give the idea of what I am looking for)
Options[dsolve] = {"ic"_List -> {}, "hint"_String -> ""};
This way I do not have to add explicit check, because the call dsolve[y''[x] + y[x] == 1, y[x], x, "hint" -> 3]
will not even match in the first place and will not go through.
Any suggestion if something like this is possible?