caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* storing object in record
@ 2009-04-08  7:36 Michael
  2009-04-08  7:54 ` [Caml-list] " Jérémie Dimino
  0 siblings, 1 reply; 4+ messages in thread
From: Michael @ 2009-04-08  7:36 UTC (permalink / raw)
  To: caml-list


it seems that I'm not able to figure out how to do  this:

class baseclass = object(this)
  method asBase = (this :> baseclass)
(* ... *)	
end;;

class ex = object inherit baseclass method name = "ex" end

type state_rec = { mutable state:  'a. #baseclass as 'a };;


this gives an error:

let x = new ex;;

{ state = x };;
Error: This field value has type ex which is less general than
         'a. #baseclass as 'a

or:

{ state = x#asBase };;
Error: This field value has type baseclass which is less general than
         'a. #baseclass as 'a

or:

type state_rec = { mutable state:  'a. < asBase: baseclass;..> as 'a };;
gives:
Error: Unbound type parameter ..


what I'm missing here?

cheers
 Michael




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

* Re: [Caml-list] storing object in record
  2009-04-08  7:36 storing object in record Michael
@ 2009-04-08  7:54 ` Jérémie Dimino
  2009-04-08 10:29   ` Michael
  0 siblings, 1 reply; 4+ messages in thread
From: Jérémie Dimino @ 2009-04-08  7:54 UTC (permalink / raw)
  To: Michael; +Cc: caml-list

Michael wrote:
> it seems that I'm not able to figure out how to do  this:
> 
> class baseclass = object(this)
>   method asBase = (this :> baseclass)
> (* ... *)	
> end;;
> 
> class ex = object inherit baseclass method name = "ex" end
> 
> type state_rec = { mutable state:  'a. #baseclass as 'a };;

There is no value of type: forall 'a. #baseclass as 'a.

I think what you want to do is:

  type state_rec = { mutable state: exists 'a. #baseclass as 'a };;

You can actually do this with objects:

  type state_rec = { mutable state : baseclass }

  { state = (x :> baseclass) }

cheers,
Jérémie


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

* Re: [Caml-list] storing object in record
  2009-04-08  7:54 ` [Caml-list] " Jérémie Dimino
@ 2009-04-08 10:29   ` Michael
  2009-04-08 11:36     ` Jérémie Dimino
  0 siblings, 1 reply; 4+ messages in thread
From: Michael @ 2009-04-08 10:29 UTC (permalink / raw)
  To: Jérémie Dimino; +Cc: caml-list

Am Wednesday 08 April 2009 09:54:48 schrieben Sie:

> There is no value of type: forall 'a. #baseclass as 'a.

is this similar? :

 type 'a xx = 'a constraint 'a = < asBase: baseObject; .. > ;;

(why does: type 'a xx = < asBase: baseObject; ..>  as 'a  not work instead? )


 type 'a r = { state: 'a xx };;

gives:
type 'a r = { state : 'a xx; } constraint 'a = < asBase : baseObject; .. >


so the record holds objects with an asBase method and more, isn't this the 
same as #baseObject?


 Michael


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

* Re: [Caml-list] storing object in record
  2009-04-08 10:29   ` Michael
@ 2009-04-08 11:36     ` Jérémie Dimino
  0 siblings, 0 replies; 4+ messages in thread
From: Jérémie Dimino @ 2009-04-08 11:36 UTC (permalink / raw)
  To: Michael; +Cc: caml-list

Le mercredi 08 avril 2009 à 12:29 +0200, Michael a écrit :
> > There is no value of type: forall 'a. #baseclass as 'a.
> 
> is this similar? :
> 
>  type 'a xx = 'a constraint 'a = < asBase: baseObject; .. > ;;
> 
> (why does: type 'a xx = < asBase: baseObject; ..>  as 'a  not work instead? )

No, [type 'a xx = 'a constraint 'a = < asBase: baseObject; .. >] just
means that ['a] must match [< asBase : baseObject; .. >]. Actually
constraints are just inlined:

  # type 'a t = 'a constraint 'a = _ * _;;
  type 'a t = 'a constraint 'a = 'b * 'c
  # let f (x : 'a t) = ();;
  val f : ('a * 'b) t -> unit = <fun>

If you write [type 'a xx = < asBase: baseObject; ..>] (with or without
[as 'a]), then you have an unbound variable in the type definition (the
[..]).

>  type 'a r = { state: 'a xx };;
> 
> gives:
> type 'a r = { state : 'a xx; } constraint 'a = < asBase : baseObject; .. >
> 
> 
> so the record holds objects with an asBase method and more, isn't this the 
> same as #baseObject?

[#baseObject] is an abbreviation for [< asBase : baseObject; .. >].

Jérémie


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

end of thread, other threads:[~2009-04-08 11:36 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-04-08  7:36 storing object in record Michael
2009-04-08  7:54 ` [Caml-list] " Jérémie Dimino
2009-04-08 10:29   ` Michael
2009-04-08 11:36     ` Jérémie Dimino

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).