Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] Variant parameterized method?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Brian Hurt <brian.hurt@q...>
Subject: [Caml-list] Variant parameterized method?

What I want to do is write a class interface like:

class virtual ['a] foo :
	method virtual doit : 'a
	method map : 'b. ('a -> 'b) -> 'b foo

The map function returns a new foo (not necessarily a new member of 
whatever derived from foo class the function is actually being called on) 
parameterized on the variant type.  This would be implemented more or less 

class ['a] mapped_foo f_init e_init =
	val f = f_init
	val e = e_init
	method doit = f (e#doit)
	method map g =
		let h x = g (f x) in
		mapped_foo g e

The first prototype, when I try to compile it, returns with:

File "foo.mli", line 2, characters 6-100:
The abbreviation foo is used with parameters 'a foo
wich are incompatible with constraints 'b foo

[sic on the wich- minor typo there]. It works just fine if I change map to 
return a 'b list, or 'b array, or 'b Stack.t- it seems to be just 'b foo 
that's a problem.  Am I missing something?  If not, what is the suggested 
way for doing this?  I need map as a member function- it needs to behave 
differently on different types, and as I don't have reflextion, I need it 
to be a member function.  I'd prefer to keep it as a class for taste 

Help is appreciated.


To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: