* class constraints
@ 2009-03-27 14:06 Jacques Le Normand
2009-03-27 14:22 ` [Caml-list] " Peng Zang
0 siblings, 1 reply; 2+ messages in thread
From: Jacques Le Normand @ 2009-03-27 14:06 UTC (permalink / raw)
To: caml-list caml-list
[-- Attachment #1: Type: text/plain, Size: 505 bytes --]
Dear list,
I'm having a little problem understanding constraints in objects. Consider
the following code:
class type foo =
object
method get_parent : foo
end
class type foo2 =
object
constraint foo2 = #foo
method baz : float
method get_parent : foo2
end
it gives me the following error:
The abbreviation foo2 expands to type < baz : float; get_parent : foo2 >
but is used with type #foo
but foo2 is clearly a subtype of foo. What am I doing wrong? I'm using ocaml
3.10.2
--Jacques Le Normand
[-- Attachment #2: Type: text/html, Size: 593 bytes --]
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [Caml-list] class constraints
2009-03-27 14:06 class constraints Jacques Le Normand
@ 2009-03-27 14:22 ` Peng Zang
0 siblings, 0 replies; 2+ messages in thread
From: Peng Zang @ 2009-03-27 14:22 UTC (permalink / raw)
To: caml-list; +Cc: Jacques Le Normand
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Perhaps you mean this?
class type foo = object('self)
method get_parent : 'self
end
class type foo2 = object('self)
constraint foo2 = #foo
method baz : float
method get_parent : 'self
end
The error you get is because the first get_parent returns a foo, while the
second returns a foo2. While you can write a function that takes "foo or
foo2" or really anything that is "foo-able", but you can't write a function
that returns "foo or foo2". When it comes to return types you have to choose
what to return. At least.. that's how I understand it.
Peng
On Friday 27 March 2009 10:06:03 am Jacques Le Normand wrote:
> Dear list,
> I'm having a little problem understanding constraints in objects. Consider
> the following code:
>
> class type foo =
> object
> method get_parent : foo
> end
>
> class type foo2 =
> object
> constraint foo2 = #foo
> method baz : float
> method get_parent : foo2
> end
>
> it gives me the following error:
>
> The abbreviation foo2 expands to type < baz : float; get_parent : foo2 >
> but is used with type #foo
>
> but foo2 is clearly a subtype of foo. What am I doing wrong? I'm using
> ocaml 3.10.2
> --Jacques Le Normand
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.7 (GNU/Linux)
iD8DBQFJzOEhfIRcEFL/JewRAknhAJ46Gn8jv6azaR0aVJZtZo/GENIlOACfUOA4
lcS+swOu0JQ4ewvtvjKAFkQ=
=nt3C
-----END PGP SIGNATURE-----
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2009-03-27 14:22 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-03-27 14:06 class constraints Jacques Le Normand
2009-03-27 14:22 ` [Caml-list] " Peng Zang
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox