Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Syntax for multiple subtyping constraints?
[ 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@k...>
Subject: Re: [Caml-list] Syntax for multiple subtyping constraints?
From: Brian Smith <brian-l-smith@uiowa.edu>

> Please consider the following:
> 
> # class virtual x = object method virtual foo : int    end
>    class virtual y = object method virtual bar : string end
>    class z =
>      object
>       method foo = 2
>       method bar = "there"
>      end;;
> 
> How can I assert that class "z" is supposed to be a subtype of class x 
> and class y? In other words, how can I assert that (some_z : z :> x) and 
> (some_z : z :> y) will always be valid?

You have many ways to force (self : #x) and (self : #y).

class z = object ((self : #x) : #y)
  method foo = 2
  method bar = "there"
end
class z = object (self : 'a)
  constraint 'a = #x
  constraint 'a = #y
  method foo = 2
  method bar = "there"
end
class z = object
  inherit x
  inherit y
  method foo = 2
  method bar = "there"
end

The last one uses the fact x and y are virtual classes, rather than
class types.

Note that none of these guarantee a subtyping relation, only a
matching one. Fortunately, as long as the type of self does not appear
in a contravariant position in x or y, this relation is included in
subtyping.

Jacques Garrigue
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners