caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* oo type question
@ 2008-03-06 12:52 Michael Wohlwend
  2008-03-06 13:18 ` [Caml-list] " Peng Zang
  0 siblings, 1 reply; 6+ messages in thread
From: Michael Wohlwend @ 2008-03-06 12:52 UTC (permalink / raw)
  To: caml-list


I try to do the following:

I have some objects (all with an id method):
let o1 = object method id = 1  method hi = "hi" end
let o2 = object method id = 2  method ho = "ho" end

and now:

class store = object 
	val mutable  ids = []
	method add o =  ids <- o#id :: ids
end

this doesn't work, since the hidden type variable in the argument to add (I 
think). But why can't the compiler just set o to be of type < obj:int; ..> ?.
So I try:
class ['a] store = object 
	val mutable  ids = []
	method add (o: 'a) =  ids <- o#id :: ids
end;;

...but...

let s = new store ;;
give the type:
val s : < id : '_a; _.. > store = <obj>

so this doesn't work:

s#add o1;
s#add o2

his expression has type < hi : string; id : int > but is here used with type
  < ha : string; id : int >
The second object type has no method ho

If I define a function, say:
let f o = o#id;;
I get :
val f : < id : 'a; .. > -> 'a = <fun>
so this works:
f o1; f o2


why does this not work with methods?

cheers
 Michael


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

end of thread, other threads:[~2008-03-10  8:42 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-03-06 12:52 oo type question Michael Wohlwend
2008-03-06 13:18 ` [Caml-list] " Peng Zang
2008-03-06 15:45   ` Michael Wohlwend
2008-03-06 16:58     ` Peng Zang
2008-03-10  8:20     ` Jacques Garrigue
2008-03-10  8:38       ` Michael Wohlwend

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