Navindra Umanee wrote:

>You know exactly what a type is and what it means, data is given a
>specific type and not one or more types, although functions may handle
>different types.  I don't think this is duck typing.
>
The only difference is that the interpreter/compiler can infer the types 
during compile time, and doesn't wait until run time like Ruby.

If I enter

- val s = "foo" ^ "bar";

The interpreter infers that, s now refers to a string

val s = "foobar" : string

because it knows the type of the operator function '^'

- op^;
val it = fn : string * string -> string

If I now do:

- val s = s + 1;

the interpreter's statical type checker finds out that the + operator 
isn't defined on string * int and rejects the program. Ruby would throw 
an exception at run time, if I tried to add an integer to a string.

>Ruby has a looser concept of duck typing and is type safe within that
>definition.  With duck typing, you pretty much invent a type as you
>need it.  If you need a duck, you look for a duck.  I'm not sure if
>you can or would want to call that strong typing.
>
The same happens in ML, only that this can be done earlier at compile 
time. New types are invented all the time in ML:

- fun double x = 2 * x;
val double = fn : int -> int

The interpreter just invented the new functional type int -> int, 
because that's what it has deduce as the type for the function double. 
(In ML functions are first class citizens.) That doesn't look like duck 
typing right now, because this function is too boring. Let's try to 
compute the length of a list instead:

- fun len []     = 0
= |   len (x::l) = 1 + len l;
val len = fn : 'a list -> int

Now a polymorphic type 'a list -> int is deduced, you could also call it 
a duck type ;). For the length of the list, it isn't important, what 
type of elements it contains, it's only important how many (an integer 
value) it are:

- len [1,2,3];
val it = 3 : int
- len ["a", "b", "c"];
val it = 3 : int

If the object quacks like a list and walks like a list, it's probably a 
list and we can find out, how many members it has.

-- 
Florian Frank