* [Caml-list] Syntax for multiple subtyping constraints?
@ 2002-07-12 5:33 Brian Smith
2002-07-12 8:54 ` Jacques Garrigue
2002-07-12 13:21 ` John Max Skaller
0 siblings, 2 replies; 3+ messages in thread
From: Brian Smith @ 2002-07-12 5:33 UTC (permalink / raw)
To: OCaml Mailing list
Hello everbody,
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? I tried:
# class z =
object (self : #x, self : #y)
method foo = 2
method bar = "there"
end;;
Characters 29-30:
object (self : #x, self : #y)
^
Syntax error
But, it only works for one supertype, e.g.:
class z =
object (self : #x) (* or (self : #y) *)
method foo = 2
method bar = "there"
end;;
I know that I could just leave it out and everything would work, but I
want to specify that z is supposed to be a subtype of both x and y so
that, if I screw up the definition of class z, the compiler will give me
an error. For example, if I didn't define the method "bar" in class "z",
I want to get the error:
This class type should be virtual
The following methods are undefined : bar
Thanks,
Brian
-------------------
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
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Caml-list] Syntax for multiple subtyping constraints?
2002-07-12 5:33 [Caml-list] Syntax for multiple subtyping constraints? Brian Smith
@ 2002-07-12 8:54 ` Jacques Garrigue
2002-07-12 13:21 ` John Max Skaller
1 sibling, 0 replies; 3+ messages in thread
From: Jacques Garrigue @ 2002-07-12 8:54 UTC (permalink / raw)
To: brian-l-smith; +Cc: caml-list
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
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Caml-list] Syntax for multiple subtyping constraints?
2002-07-12 5:33 [Caml-list] Syntax for multiple subtyping constraints? Brian Smith
2002-07-12 8:54 ` Jacques Garrigue
@ 2002-07-12 13:21 ` John Max Skaller
1 sibling, 0 replies; 3+ messages in thread
From: John Max Skaller @ 2002-07-12 13:21 UTC (permalink / raw)
To: Brian Smith; +Cc: OCaml Mailing list
Brian Smith wrote:
> In other words, how can I assert that (some_z : z :> x) and (some_z :
> z :> y) will always be valid?
Well, you can just encode the conversion in a function:
let x_of_z (some_z : z):x = (some_z:z::>x)
--
John Max Skaller, mailto:skaller@ozemail.com.au
snail:10/1 Toxteth Rd, Glebe, NSW 2037, Australia.
voice:61-2-9660-0850
-------------------
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
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2002-07-12 13:21 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-07-12 5:33 [Caml-list] Syntax for multiple subtyping constraints? Brian Smith
2002-07-12 8:54 ` Jacques Garrigue
2002-07-12 13:21 ` John Max Skaller
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox