Version française
Home     About     Download     Resources     Contact us    
Browse thread
class type param constraint
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jacques Garrigue <garrigue@m...>
Subject: Re: [Caml-list] class type param constraint
From: Radzevich Belevich <radzevich.belevich@gmail.com>
> class [ 'c ] a = 
>   object(self)                  
>     constraint 'c = < .. >
>     method as_c =  (self :> 'c)
>     method id = Oo.id self
>   end          ;;
> class ['a] a :
>   object ('a)
>     constraint 'a = < as_c : 'a; id : int; .. >
>     method as_c : 'a
>     method id : int
>   end
> ================
> 
> Why type 'c must have methods "as_c" and "id" ? 

This may be a bit confusing, but the .. in your constraint on 'c means
that no actual subtyping will occur when you use this type as target.
Indeed, if the constraint was left unchanged, it would mean that by
choosing an arbitrary instance, you could use the as_c method to
convert an object of type "t a" to any "t".
Since no subtyping occurs, (self :> 'c) is just equivalent to
(self : 'c) (i.e. unify 'c with the type of self).
As a result, you get
  object ('a)
    ...
    method as_c : 'a
    ...
  end

Jacques