caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* down casting ???
@ 1996-06-19  7:03 FAVRE Jean-Marie
  1996-06-20 17:05 ` Didier Remy
  1996-06-20 19:11 ` Jerome Vouillon
  0 siblings, 2 replies; 3+ messages in thread
From: FAVRE Jean-Marie @ 1996-06-19  7:03 UTC (permalink / raw)
  To: caml-list


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 ?

I first believed that using pattern like (x : t1) -> can do the job,
(a kind of typecase construction as it is the case in modula 3).

is it possible ???


	Jean-marie

-- 





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

* Re: down casting ???
  1996-06-19  7:03 down casting ??? FAVRE Jean-Marie
@ 1996-06-20 17:05 ` Didier Remy
  1996-06-20 19:11 ` Jerome Vouillon
  1 sibling, 0 replies; 3+ messages in thread
From: Didier Remy @ 1996-06-20 17:05 UTC (permalink / raw)
  To: FAVRE Jean-Marie; +Cc: caml-list


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

Yes, the coercion is unsafe and a dynamic check is needed.  As you mention,
this could be implemented with dynamics (values carrying their types) and a
typecase.

> When one use collections of object, this kind of feature is useful. 

Yes, this is a typical  example where such a construction would be
convenient. The  way to solve it now is to declare a data type with one
constructor for each different kind of objet that you want to retreive
from the collection (as you would do in ML without objects). 

> (a kind of typecase construction as it is the case in modula 3).
> is it possible ???

Yes, it is. However, dynamics and typecase complicate the language, its
semantics, and its implementation; so, we preferred not to include them.

    Didier.





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

* Re: down casting ???
  1996-06-19  7:03 down casting ??? FAVRE Jean-Marie
  1996-06-20 17:05 ` Didier Remy
@ 1996-06-20 19:11 ` Jerome Vouillon
  1 sibling, 0 replies; 3+ messages in thread
From: Jerome Vouillon @ 1996-06-20 19:11 UTC (permalink / raw)
  To: FAVRE Jean-Marie; +Cc: caml-list



> 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)];;





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

end of thread, other threads:[~1996-06-21  9:03 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-06-19  7:03 down casting ??? FAVRE Jean-Marie
1996-06-20 17:05 ` Didier Remy
1996-06-20 19:11 ` Jerome Vouillon

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