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

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Michal Moskal <malekith@p...>
Subject: Re: [Caml-list] object
On Mon, Feb 17, 2003 at 12:14:44PM +0300, inv2002 wrote:
> class virtual obj = object
>     ...
>   end
> 
> class a_obj = object
>   inherit obj
>   method a_fun = ...
> end
> 
> class b_obj = object
>   inherit obj
>   method b_fun = ...
> end
> 
> class object_list = object
>   val ht = Hashtbl.create 5
> 
>   method add: 'a. string -> (#obj as 'a) -> unit = fun key o ->
>     Hashtbl.add ht key (o :> obj) (* i don't know right way, how to store objects *)
> 
>   method find key =
>     Hashtbl.find ht key
> 
> end
> 
> i want to store a_obj and b_obj in object_list,
> after find it and execute a_fun method
> if the object is a_obj

How do you know object is really of type a_obj?

There are few solutions:

1. type a_or_b_obj = A_obj of a_obj | B_obj of b_obj

method add k o = Hashtbl.add ht k o

foo#add (A_obj a)
foo#add (B_obj b)

2. add a_fun and b_fun with ,,assert false'' like implementation
3. use Obj.magic

First solution gives you static typing, second gives you dynamic typing,
and third -- no typing.

-- 
: Michal Moskal ::::: malekith/at/pld-linux.org :  GCS {C,UL}++++$ a? !tv
: PLD Linux ::::::: Wroclaw University, CS Dept :  {E-,w}-- {b++,e}>+++ h
-------------------
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