caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Polymorphic method question
@ 2006-07-10 19:21 brogoff
  2006-07-10 20:05 ` [Caml-list] " Richard Jones
  2006-07-11  2:09 ` Jacques Garrigue
  0 siblings, 2 replies; 13+ messages in thread
From: brogoff @ 2006-07-10 19:21 UTC (permalink / raw)
  To: caml-list

Hi,
    I'm sure I'll slap my forehead in disgust when someone lifts the
scales from my eyes, but I'm once again perplexed by a type error
from OCaml when using objects. Can someone tell me why I get the
error from the second case (with the classes connected by "and") when
the first case is OK?

   BTW, This example was distilled from one in which I could not cleanly
remove the class recursion, one involving an "extensible visitor" in
which there is a recursion between the visited and visitor classes.
Assuming there's an obvious answer to my first question, is there a
nice workaround in this case?

brogoff@denali[tricks]$ ocaml
        Objective Caml version 3.09.2

# class virtual ['a] bar =
    object
      method virtual get : 'a
    end;;
      class virtual ['a] bar : object method virtual get : 'a end
# class virtual foobar =
    object
      method virtual f : 'a . 'a bar -> 'a
    end;;
      class virtual foobar : object method virtual f : 'a bar -> 'a end
# class virtual ['a] bar =
    object
      method virtual get : 'a
    end
  and foobar =
    object
      method virtual f : 'a . 'a bar -> 'a
    end;;
              Characters 115-132:
        method virtual f : 'a . 'a bar -> 'a
                           ^^^^^^^^^^^^^^^^^
This type scheme cannot quantify 'a :
it escapes this scope.

-- 
-- Brian



^ permalink raw reply	[flat|nested] 13+ messages in thread
* [Caml-list] polymorphic method question
@ 2002-08-21 21:49 nadji
  2002-08-21 22:57 ` Jacques Garrigue
  0 siblings, 1 reply; 13+ messages in thread
From: nadji @ 2002-08-21 21:49 UTC (permalink / raw)
  To: caml-list

Hi,

I am puzzled by this behaviour, can a guru explain me ?
I have two classes
class type ['a] basic = object
  method foo : 'a
end;;

 class ['a] toto (a:'a) = object
  method foo = a
  method bar = ()
end;;

I want to do a class gentoto :
class gentoto = object
  method gen : 'a . 'a -> 'a basic =
    fun x -> (new toto x :> 'a basic )
end;;
But the compiler complains that :
      Characters 64-98:
      fun x -> (new toto x :> 'a basic )
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This method has type 'a -> 'a basic which is less general than
  'b. 'b -> 'b basic

... but if I do :
let newtotoasbasic x = (new toto x :> 'a basic );;

class gentoto =
object
  method gen : 'a . 'a -> 'a basic =
(*     fun x -> (new toto x :> 'a basic ) *)
    newtotoasbasic
end;;

The compiler agrees with me ...
What is wrong with the first declaration ?

Thanks,
Nadji

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

end of thread, other threads:[~2006-07-12 19:26 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-07-10 19:21 Polymorphic method question brogoff
2006-07-10 20:05 ` [Caml-list] " Richard Jones
2006-07-10 23:25   ` brogoff
2006-07-11  2:24     ` skaller
2006-07-11  4:56       ` brogoff
2006-07-11  2:09 ` Jacques Garrigue
2006-07-11  5:22   ` brogoff
2006-07-11  7:32     ` Jacques Garrigue
2006-07-11 18:20       ` brogoff
2006-07-12  0:37         ` Jacques Garrigue
2006-07-12 19:26           ` brogoff
  -- strict thread matches above, loose matches on Subject: below --
2002-08-21 21:49 [Caml-list] polymorphic " nadji
2002-08-21 22:57 ` Jacques Garrigue

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