Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[Caml-list] Problem with recursive class and non-class types

2 views
Skip to first unread message

Goswin von Brederlow

unread,
May 20, 2010, 1:12:02 AM5/20/10
to caml...@yquem.inria.fr
Hi,

I want to define the two types below:

type foo = { bar : bar; }
class bar = object val mutable foo : foo list = [] end

Is there another way of doing this other than:

# type 'a foo = { bar : 'a; }
class bar = object val mutable foo : #bar foo list = [] end;;
type 'a foo = { bar : 'a; }
class bar : object val mutable foo : #bar foo list end

I don't want any 'a foo other than 'a = #bar. It is too easy to create a
baz foo and then much later get a type error when trying to use it as
bar foo. I want the error where the foo gets created.

MfG
Goswin

_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

Jacques Garrigue

unread,
May 20, 2010, 3:39:48 AM5/20/10
to goswi...@web.de, caml...@yquem.inria.fr
From: Goswin von Brederlow <goswi...@web.de>

> I want to define the two types below:
>
> type foo = { bar : bar; }
> class bar = object val mutable foo : foo list = [] end
>
> Is there another way of doing this other than:
>
> # type 'a foo = { bar : 'a; }
> class bar = object val mutable foo : #bar foo list = [] end;;
> type 'a foo = { bar : 'a; }
> class bar : object val mutable foo : #bar foo list end

The alternative is to use a recursive module, but this is actually
more verbose.

module rec M : sig
type foo = { bar : M.bar; }
class bar : object val mutable foo : foo list end
end = struct
type foo = { bar : M.bar; }


class bar = object val mutable foo : foo list = [] end

end

You can avoid a bit of the verboseness by splitting types and values,
since recursive modules built only from types require no duplication.

module rec M : sig
type foo = { bar : M.bar; }
class type bar = object val mutable foo : foo list end
end = M

class bar : M.bar = object val mutable foo : M.foo list = [] end

You still need to provide an explicit interface for bar.

Hope this helps,

Jacques Garrigue

Goswin von Brederlow

unread,
May 21, 2010, 2:01:24 PM5/21/10
to Jacques Garrigue, caml...@yquem.inria.fr
Jacques Garrigue <garr...@math.nagoya-u.ac.jp> writes:

Thanks, it does. It isn't nice but it does solve the problem. Now I have
to decide what I live with. 'a foo uglyness or module rec uglyness.

It is too bad a simple

type foo = ...
and class bar = ...

doesn't work.

MfG
Goswin

0 new messages