* [Caml-list] How to downcast in OCAML
@ 2002-09-21 15:09 Tim Freeman
2002-09-23 9:49 ` Andreas Rossberg
2002-09-25 7:10 ` Jean-Christophe Filliatre
0 siblings, 2 replies; 3+ messages in thread
From: Tim Freeman @ 2002-09-21 15:09 UTC (permalink / raw)
To: caml-list
For what it's worth (which may not be much), it is possible to write
classes in OCAML that are downcastable. Here's how.
--
Tim Freeman
tim@fungible.com
GPG public key fingerprint ECDF 46F8 3B80 BB9E 575D 7180 76DF FE00 34B1 5C78
module type Foo = sig
class type superclass = object
method data: exn
end
class subclass_1: string -> object
inherit superclass
method s: string
end
exception Subclass_1 of subclass_1
class subclass_2: int -> object
inherit superclass
method i: int
end
exception Subclass_2 of subclass_2
val x: superclass
end
module Foo: Foo = struct
class type superclass = object
method data: exn
(* You can put more methods here if you like. *)
end
class subclass_1_impl (s: string) (makeexn: subclass_1_impl -> exn) =
object (self: 'self)
constraint 'self = #superclass
method data: exn = makeexn (self :> subclass_1_impl)
method s: string = s
end
exception Subclass_1 of subclass_1_impl
class subclass_1 (s: string) = subclass_1_impl s (fun sc -> Subclass_1 sc)
class subclass_2_impl (i: int) (makeexn: subclass_2_impl -> exn) =
object (self: 'self)
constraint 'self = #superclass
method data: exn = makeexn (self :> subclass_2_impl)
method i: int = i
end
exception Subclass_2 of subclass_2_impl
class subclass_2 (i: int) = subclass_2_impl i (fun sc -> Subclass_2 sc)
let _ = Random.self_init ()
let x: superclass =
if Random.bits () mod 2 = 0 then
(new subclass_1 "blort" :> superclass)
else
(new subclass_2 17 :> superclass)
let _ =
match x#data with
Subclass_1 s1 -> Format.printf "s is %s.\n@?" s1#s
| Subclass_2 s2 -> Format.printf "i is %d.\n@?" s2#i
| _ -> failwith "Downcast failed"
end
-------------------
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] 3+ messages in thread
* Re: [Caml-list] How to downcast in OCAML
2002-09-21 15:09 [Caml-list] How to downcast in OCAML Tim Freeman
@ 2002-09-23 9:49 ` Andreas Rossberg
2002-09-25 7:10 ` Jean-Christophe Filliatre
1 sibling, 0 replies; 3+ messages in thread
From: Andreas Rossberg @ 2002-09-23 9:49 UTC (permalink / raw)
To: caml-list
Tim Freeman wrote:
>
> For what it's worth (which may not be much), it is possible to write
> classes in OCAML that are downcastable. Here's how.
>
> [trick using exception constructors]
Unfortunately, your scheme does not scale to class hierarchies deeper
than 1. Consider for example adding `subsubclass' as a subclass of
`subclass_1'. Then you might want to downcast from `superclass' to
`subclass_1', but your object actually is a `subsubclass'. Your downcast
will fail. You need hierarchical exception constructors to make that
work.
- Andreas
--
Andreas Rossberg, rossberg@ps.uni-sb.de
"Computer games don't affect kids; I mean if Pac Man affected us
as kids, we would all be running around in darkened rooms, munching
magic pills, and listening to repetitive electronic music."
- Kristian Wilson, Nintendo Inc.
-------------------
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] 3+ messages in thread
* Re: [Caml-list] How to downcast in OCAML
2002-09-21 15:09 [Caml-list] How to downcast in OCAML Tim Freeman
2002-09-23 9:49 ` Andreas Rossberg
@ 2002-09-25 7:10 ` Jean-Christophe Filliatre
1 sibling, 0 replies; 3+ messages in thread
From: Jean-Christophe Filliatre @ 2002-09-25 7:10 UTC (permalink / raw)
To: Tim Freeman; +Cc: caml-list
Tim Freeman writes:
>
> For what it's worth (which may not be much), it is possible to write
> classes in OCAML that are downcastable. Here's how.
Just to mention it: there is a camlp4 extension of ocaml providing
downcast, called coca-ml, and it is available here:
http://www.pps.jussieu.fr/~emmanuel/Public/Dev/coca-ml/index-en.html
--
Jean-Christophe
-------------------
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] 3+ messages in thread
end of thread, other threads:[~2002-09-25 7:23 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-09-21 15:09 [Caml-list] How to downcast in OCAML Tim Freeman
2002-09-23 9:49 ` Andreas Rossberg
2002-09-25 7:10 ` Jean-Christophe Filliatre
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox