September 22, 1999 | Strong Typing | Slide #17 |
The big deal is that you do not need to declare types!
fun fact 0 = 1 | fact n = n * fact(n-1);
Compiler says to itself:
0 is an int, so n must be also.
n-1 checks out OK
* requires two arguments of the same type, so fact must return an int
The return value of 1 is consistent with that
OK, everything checks out!
Then it prints:
val fact = fn : int -> int
Then you look at the type. Is int -> int what you expected?
Yes? OK then!
If not, your program almost certainly has a bug.
Next | Copyright © 1999 M-J. Dominus |