caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@kurims.kyoto-u.ac.jp>
To: malekith@pld-linux.org
Cc: inv2002@yandex.ru, caml-list@inria.fr
Subject: Re: [Caml-list] object
Date: Wed, 19 Feb 2003 09:23:42 +0900	[thread overview]
Message-ID: <20030219092342L.garrigue@kurims.kyoto-u.ac.jp> (raw)
In-Reply-To: <20030217172225.GB12324@roke.freak>

From: Michal Moskal <malekith@pld-linux.org>
> On Mon, Feb 17, 2003 at 12:14:44PM +0300, inv2002 wrote:
> > class a_obj = ...
> > class b_obj = ...
> > 
> > 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.

Please, never suggest using Obj.magic on this list.
(Maybe we should have a special filter so that messages containing the
words Obj.magic are reviewed by hand)
Using Obj.magic in a user program is throwing away all the safety
ensured by caml, going back to the C level. And gdb will not help you.

If you look at the caml list archives with the keyword "downcast" you
will see a whole litterature about how to solve this kind of problems

http://pauillac.inria.fr/bin/wilma_glimpse/caml-list?query=downcast

Solution (1) and (2) above are good enough for simple cases.

If you don't like the restrictions caused by solution (1), my prefered
answer uses parametric classes:

class virtual ['a] obj = object
    method virtual real : 'a
    ...
  end

class ['a] a_obj = object (self)
  inherit ['a] obj
  method real = `Ta self
  method a_fun = ...
end

class ['a] b_obj = object
  inherit ['a] obj
  method real = `Tb self
  method b_fun = ...
end

This way you can get back your original type by pattern matching on
o#real. OO purists would say that it does not work well with
subtyping, but if your problem is about subtyping then solution (2)
above might be better.

Note also that it may just be that your example does not need objects
at all.  This kind of by-case reasonning is often better handled by
simple sum types.

Cheers,

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


      reply	other threads:[~2003-02-19  0:23 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-02-17  9:14 inv2002
2003-02-17 17:22 ` Michal Moskal
2003-02-19  0:23   ` Jacques Garrigue [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=20030219092342L.garrigue@kurims.kyoto-u.ac.jp \
    --to=garrigue@kurims.kyoto-u.ac.jp \
    --cc=caml-list@inria.fr \
    --cc=inv2002@yandex.ru \
    --cc=malekith@pld-linux.org \
    /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;
as well as URLs for NNTP newsgroup(s).