From: Steven@..., Grady@..., "From@...": Date: 2003-11-20T13:12:15+09:00 Subject: Ocaml's typing (was Re: "stereotyping") Austin Ziegler writes: >Consider generics in Java (templates in C++). These effectively >introduce the sort of dynamicity that is found in Perl, Python, and >Ruby. Instead of having to do: > int max(int x, int y) { return x >= y ? x : y } > float max(float x, float y) { return x >= y ? x : y } > double max(double x, double y) { return x >= y ? x : y } >You can do: > def max(x, y); x >= y ? x : y; end >I know which I would prefer, and C++'s move toward generics is a >halting step toward that step. BTW, to see a different point of view, you might investigate Ocaml (or other ML languages, or Haskell). Ocaml is strongly, statically type-checked. Here is max: let max x y = if x > y then x else y (Note that there are no type declarations!) # max 3 4;; - : int = 4 # max 3.0 4.0;; - : float = 4 Why does this work? How can max return an int the first time, and a float the second? Because the type declaration for max (as inferred by the compiler) is: max : 'a -> 'a -> 'a = That is, max takes two arguments, of type 'a (whatever that happens to be), and returns something of the same type. So when it is called with two ints, it returns an int, and when called with two floats, it returns a float. And when called with one of each? The _compiler_ generates an error: # max 3 4.0;; ^^^ This expression has type float but is here used with type int Compile-time type-checking, with no type annotations! My point being, C++ and Java are not the extent of statically-typed languages. In fact, I suspect what most people here hate is not static typing, but mandatory type annotation. Someone also said that types are for compilers, not developers. Wrong: They're for both. The compiler can generate better code with types; developers can use it as documentation, and can express useful constraints. That's why Ocaml supports type declarations, even though you can write programs without them. (Oh, just to be clear -- I love Ruby.) -- Steven "I buried Flanders."