Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Jerome Vouillon <vouillon@clipper.ens.fr>
To: FAVRE Jean-Marie <Jean-Marie.Favre@imag.fr>
Cc: caml-list@pauillac.inria.fr
Subject: Re: down casting ???
Date: Thu, 20 Jun 1996 21:11:49 +0200 (MET DST)	[thread overview]
Message-ID: <Pine.3.89.9606202024.A9351-0100000@vedette> (raw)
In-Reply-To: <9606190903.ZM4853@tassili>



> the :> operator make possible to coerce an expression from a type t1 to a
> super type t (this coercion is safe), but how to go from t to t1 ???
> (ok, a dynamic check is need, this operation can raise an exception)
> 
> When one use collections of object, this kind of feature is useful. isn't it ?

There are some programming tricks that lessen the need for such a feature.
Here are two examples.

     Jerome


class virtual print_interf () =
  virtual print : unit
  (* Possibly some more methods... *)
end;;
class opt_print () =
  method print_interf = (None : print_interf option)
end;;
class virtual can_print () as self =
  inherit opt_print () inherit print_interf ()
  method print_interf = Some (self :> print_interf)
end;;

class a () =
  inherit opt_print ()
end;;
class b () =
  inherit can_print ()
  method print = print_string "xxx"
end;;
let x1 = new a ();;
let x2 = new b ();;
List.iter
  (function x -> match x#print_interf with None -> () | Some y -> y#print)
  [x1; (x2 :> a)];;

(**********************************************************************)

class virtual int_interf () = virtual x : int end;;
class virtual string_interf () = virtual x : string end;;
class virtual visitor () =
  virtual int : int_interf -> unit
  virtual string : string_interf -> unit
end;;
class virtual visited () =
  virtual accept : visitor -> unit
end;;
class virtual int_wrapper () as self =
  inherit visited ()
  inherit int_interf ()
  method accept visitor = visitor#int (self :> int_interf)
end;;
class virtual string_wrapper () as self =
  inherit visited ()
  inherit string_interf ()
  method accept visitor = visitor#string (self :> string_interf)
end;;

class printer () =
  inherit visitor ()
  method int obj = print_int obj#x
  method string obj = print_string obj#x
end;;
class a x0 =
  inherit int_wrapper ()
  val x = x0
  method x = x
end;;
class b x0 =
  inherit string_wrapper ()
  val x = x0
  method x = x
end;;
let p = new printer ();;
let x1 = new a 7;;
let x2 = new b "xxx";;
List.iter (function x -> x#accept p) [(x1 :> visited); (x2 :> visited)];;





      parent reply	other threads:[~1996-06-21  9:03 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-06-19  7:03 FAVRE Jean-Marie
1996-06-20 17:05 ` Didier Remy
1996-06-20 19:11 ` Jerome Vouillon [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.3.89.9606202024.A9351-0100000@vedette \
    --to=vouillon@clipper.ens.fr \
    --cc=Jean-Marie.Favre@imag.fr \
    --cc=caml-list@pauillac.inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox