English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
Object typing
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2005-07-11 (08:22)
From: Jacques Garrigue <garrigue@m...>
Subject: Re: [Caml-list] Object typing
From: Remi Vanicat <remi.vanicat@gmail.com>

> Notice that one can do better than this :
> # class [+'a] entity =
> object
>  method is_a (v:'a) = (v = `Entity)
> end;;
>      class ['a] entity :
>   object constraint 'a = [> `Entity ] method is_a : 'a -> bool end
> # let e = new entity;;
> val e : [> `Entity ] entity = <obj>
> now we don't have an _ type, and there is no need to close it before
> the end of the program.

Thank you for finding a serious bug in the variance check!
Didn't it strike you as strange that a method parameter could be
declared with a covariant type? This goes clearly against the
The problem was that the variance check omitted the row-variable in
variants, so that if a variant constraint is used on a type parameter,
any variance is accepted...

After fixing this in CVS for 3.08.4, you get rightfully:
  In this definition, a type variable has a variance that is not reflected
  by its occurence in type parameters.
or in 3.09
  In this definition, expected parameter variances are not satisfied

If you really want to make it covariant, you should define is_a out of
the class:

# class [+'a] entity =
    method classes : 'a = [`Entity]
class ['a] entity :
  object constraint 'a = [> `Entity ] list method classes : 'a end
# let e = new entity;;
val e : [> `Entity ] list entity = <obj>
# let is_a ob cl = List.mem cl ob#classes;;
val is_a : < classes : 'a list; .. > -> 'a -> bool = <fun>

(And be careful about the above bug before 3.08.4.)

Jacques Garrigue

P.S. I really think that the variance check is now OK, but if you find
strange behaviours in typing, please report them. Particularly when
something that shouldn't type is accepted.