I'd like to make my own "typed function" in WolframScript. Suppose the type we desire is called
money, and we'd like to make a function called
doubleOurMoney which doubles any
money argument but throws an error on everything else. How do we do this?
Attempt: This isn't quite right but perhaps is a good starting point:
doubleOurMoney[money[x_Real]] := money[2 x] doubleOurMoney[money[2.0]] (* Should return money[4.], and does. *) doubleOurMoney[money["nonsense"]] (* How do we make this throw an error? *) doubleOurMoney (* How do we make this throw an error? *) doubleOurMoney["nonsense"] (* How do we make this throw an error? *) doubleOurMoney[symbol] (* How do we make this throw an error? *)
- How do we throw "type errors" in this context?
- Right now
moneyaccepts real numbers as arguments; how could we make it take any "number" as an argument (Integer, etc) but throw an error on everything else?
- Is my attempt at "typed WolframScript" an idiomatic approach to type-checking in Wolfram? If not, what is the idiomatic approach here to check that inputs to a function are sensible?