Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Re: OCAML Downcasting?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jacques GARRIGUE <garrigue@k...>
Subject: Re: [Caml-list] Re: OCAML Downcasting?
From: Damien Pous <Damien.Pous@ens-lyon.fr>

> Jacques GARRIGUE <garrigue@kurims.kyoto-u.ac.jp> wrote:
> > Just for fun, here is an example of the second approach:
> > 
> > class virtual ['a] widget = object
> >   method virtual raw : 'a
> > end 
> > class ['a] button = object (self)
> >   inherit ['a] widget
> >   method raw = `Button self
> >   method press = prerr_endline "pressed"
> > end
> 
> How to write a signature for this code ?
> 
> even "ocamlc -i" fails ! 

You're right.
I shall never post before verifying my code seriously.
The real code is:

class virtual ['a] widget = object
  method virtual raw : 'a
end

class ['a] button = object (self)
  inherit ['a] widget
  method raw = `Button (self :> 'a button)
  method press = prerr_endline "pressed"
end

class ['a] label = object (self)
  inherit ['a] widget
  method raw = `Label (self :> 'a label)
  val mutable text = ""
  method set_text s = text <- s
end

let l = [(new button :> _ widget); (new label :> _ widget)]

let f = function
    `Button b -> b#press
  | `Label l -> l#set_text "Hello" 

let test () =
  List.iter (fun o -> f o#raw) l

Without the self-cast, you cannot extend any class.

Jacques Garrigue

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