So. SML lets you redefine basic operators on basic types
February 11, 2013 Leave a comment
For instance, I could do this-
fun op-(x,y) = x+y
This makes the ‘-’ operator do integer addition. Reals? Hah, no subtracting them now! Fails type checking on anything but ints.
I can also do:
fun op-(x) = ~x;
Now, the ‘-’ operator does nothing except spawn error messages wherever it is used. Well, wherever it is used except the definition of that function. Which type checks perfectly fine. Uses of this version of ‘-’ will either fail type checking, throw up syntax errors, or both. I’m mystified as to why this function definition actually type checks, when it it guaranteed to throw up errors when used.
Redefining how operators behave can be useful, but when it can *only* be an infix operator, why allow me to redefine it to take only one argument? I discovered this mostly by accident reading library documentation and saying “hey, I wonder if this would work…”, so maybe there’s some additional syntax that can convert it to prefix or postfix? Even so, why allow the infix version to be defined to take one argument, guaranteeing a noncallable function?