30Fermer32
damnvoidLe 26/01/2006 à 19:55
Je crois que tu te trompes.

# let rec size = function
  | [] -> 0
  | h::t -> 1 + (size t);;
val size : 'a list -> int = <fun>

# [0; 1; 2; 3];;
- : int list = [0; 1; 2; 3]

# size [0; 1; 2; 3];;
- : int = 4


Pour moi celà veut dire que int list est un sous-type de 'a list vu que les fonctions qui prennent des 'a list en argument acceptent aussi des int list.

Enfin sur wikipedia:subtype, je lis :
Implementations of programming languages with subtyping fall into two general classes: inclusive implementations, in which the representation of any value of type A also represents the same value at type B if A<:B, and coercive implementations, in which a value of type A can be automatically converted into one of type B. The subtyping induced by subclassing in an object-oriented language is usually inclusive; subtyping relations that relate integers and floating-point numbers, which are represented differently, are usually coercive.

Donc l'implantation du sous-typage dans caml est inclusive. Mais pourquoi ont-ils choisi de faire ça plutôt que l'implantation coercitive qui me semble bien plus pratique ?