caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Problem with recursive class and non-class types
@ 2010-05-20  5:12 Goswin von Brederlow
  2010-05-20  7:39 ` [Caml-list] " Jacques Garrigue
  0 siblings, 1 reply; 3+ messages in thread
From: Goswin von Brederlow @ 2010-05-20  5:12 UTC (permalink / raw)
  To: caml-list

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Problem with recursive class and non-class types
  2010-05-20  5:12 Problem with recursive class and non-class types Goswin von Brederlow
@ 2010-05-20  7:39 ` Jacques Garrigue
  2010-05-21 18:01   ` Goswin von Brederlow
  0 siblings, 1 reply; 3+ messages in thread
From: Jacques Garrigue @ 2010-05-20  7:39 UTC (permalink / raw)
  To: goswin-v-b; +Cc: caml-list

From: Goswin von Brederlow <goswin-v-b@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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Problem with recursive class and non-class types
  2010-05-20  7:39 ` [Caml-list] " Jacques Garrigue
@ 2010-05-21 18:01   ` Goswin von Brederlow
  0 siblings, 0 replies; 3+ messages in thread
From: Goswin von Brederlow @ 2010-05-21 18:01 UTC (permalink / raw)
  To: Jacques Garrigue; +Cc: goswin-v-b, caml-list

Jacques Garrigue <garrigue@math.nagoya-u.ac.jp> writes:

> From: Goswin von Brederlow <goswin-v-b@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

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2010-05-21 18:01 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-05-20  5:12 Problem with recursive class and non-class types Goswin von Brederlow
2010-05-20  7:39 ` [Caml-list] " Jacques Garrigue
2010-05-21 18:01   ` Goswin von Brederlow

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).