* [Caml-list] Recursive class types (long-standing question ...)
@ 2003-12-30 21:02 William Chesters
2004-01-10 4:43 ` Jacques Garrigue
0 siblings, 1 reply; 2+ messages in thread
From: William Chesters @ 2003-12-30 21:02 UTC (permalink / raw)
To: caml-list
Here's something I've wondered about for a while and just encountered again.
How come this class ..
class virtual ['a] c = object
method virtual b: bool c
end
... gets a constraint 'a = bool? I.e. for instance this doesn't compile:
class virtual ['a] c = object
method virtual b: bool c
method virtual i: int c
(* "This type int should be an instance of type bool" *)
end
Note that the following does work in C++:
template <class a>
struct c {
virtual c<bool> b() = 0;
virtual c<int> i() = 0;
};
I guess there are workarounds but it seems unnecessary that ML should find
recursive parameterised classes harder than C++?
-------------------
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] 2+ messages in thread
* Re: [Caml-list] Recursive class types (long-standing question ...)
2003-12-30 21:02 [Caml-list] Recursive class types (long-standing question ...) William Chesters
@ 2004-01-10 4:43 ` Jacques Garrigue
0 siblings, 0 replies; 2+ messages in thread
From: Jacques Garrigue @ 2004-01-10 4:43 UTC (permalink / raw)
To: williamc; +Cc: caml-list
From: William Chesters <williamc@paneris.org>
> Here's something I've wondered about for a while and just encountered again.
> How come this class ..
>
> class virtual ['a] c = object
> method virtual b: bool c
> end
>
> ... gets a constraint 'a = bool? I.e. for instance this doesn't compile:
>
> class virtual ['a] c = object
> method virtual b: bool c
> method virtual i: int c
> (* "This type int should be an instance of type bool" *)
> end
This is almost a FAQ.
The deep reason is that ocaml object types are defined by structure,
and as a result only regular recursion is allowed. If we were to allow
polymorphic recursion in class types as required by your example, the
type could not be represented as a regular tree anymore.
(to be fully exact, in this particular case it is possible to
represent it as a regular tree, but this would break the stronger
invariant used by the compiler, which requires all occurences of a
type in its own expansion to have the same parameters)
Note that this applies only to structural types.
For defined types you can use polymorphic recursion.
type 'a t = { b: bool t }
> Note that the following does work in C++:
>
> template <class a>
> struct c {
> virtual c<bool> b() = 0;
> virtual c<int> i() = 0;
> };
>
> I guess there are workarounds but it seems unnecessary that ML should find
> recursive parameterised classes harder than C++?
In C++ classes are not equal by structure, so the second case applies.
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] 2+ messages in thread
end of thread, other threads:[~2004-01-10 4:42 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-12-30 21:02 [Caml-list] Recursive class types (long-standing question ...) William Chesters
2004-01-10 4:43 ` Jacques Garrigue
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox