Python and generic programming

Steven Bethard steven.bethard at gmail.com
Fri Oct 22 16:44:52 CEST 2004


Oliver Fromme <olli <at> haluter.fromme.com> writes:
> 
> Type checking at compile time does _not_ necessarily require
> static typing.
> 
> For a counter example, look at O'Caml.  It is a dynamically
> typed functional language (using type inference, pattern
> matchingetc.) with compile-time type checking.

I guess I don't quite understand this example.  Isn't type inference done at 
compile time?  So that at the end of compilation, each variable in an ML 
program has been assigned a type?

I've never done much more than play around with ML, but consider this example:

# let f x = if x > x then 1 else x;;
val f : int -> int = <fun>
# f 1.2;;
Characters 2-5:
  f 1.2;;
    ^^^
This expression has type float but is here used with type int

So what happens here of course is that x is inferred the type int because of 
the if-then-else statement.  And so OCaml doesn't let you pass a float to the 
function.  In a dynamically typed language, I would not expect an error -- any 
type that supports the '>' operator should work just fine in this context.

Could you explain a little more what about OCaml makes you consider it 
dynamically typed?

Steve




More information about the Python-list mailing list