caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Tiphaine.Turpin" <Tiphaine.Turpin@free.fr>
To: Caml-list List <caml-list@inria.fr>
Subject: Re: [Caml-list] Objects, dynamic cast, Obj.magic abuse and dragons
Date: Wed, 27 Feb 2008 14:26:02 +0100	[thread overview]
Message-ID: <47C564EA.4060902@free.fr> (raw)
In-Reply-To: <20080226121451.GA20860@annexia.org>

Here is another try. it has the drawback that classes need to be 
parametric, but it seems to work, and to be scalable (the added code is 
always the same). Of course, some syntactic sugar would help.

Tiphaine Turpin

Richard Jones a écrit :
> I only briefly read over this, but maybe the thing you want is an
> object memo.  There's a specialized one in lablgtk called GUtil.memo,
> but the basic source for it could be adapted:
>
>   class ['a] memo () = object
>     constraint 'a = #widget
>     val tbl = Hashtbl.create 7
>     method add (obj : 'a) =
>       Hashtbl.add tbl obj#get_id obj
>     method find (obj : widget) = Hashtbl.find tbl obj#get_id
>     method remove (obj : widget) = Hashtbl.remove tbl obj#get_id
>   end
>
> There's an example of using this if you search down for 'memo' on this
> page:
>
>   http://www.ocaml-tutorial.org/introduction_to_gtk
>
> Rich.
>
>   
#load "extLib.cma"

open ExtList
exception Class_cast_exception

let cast f o =
  match List.filter_map f o#supers with
    | [] -> raise Class_cast_exception
    | o' :: _ -> o'

class ['super] a = object (self : 'self)
  method supers : 'super list = [`a (self :> _ a)]
  method a = ()
end

class ['super] b = object (self : 'self)
  inherit ['super] a as a
  method supers = `b (self :> _ b) :: a#supers
  method b = ()
end

class ['super] c = object (self : 'self)
  inherit ['super] a as a
  method supers = `c (self :> _ c) :: a#supers
  method c = ()
end

class ['super] d = object (self : 'self)
  inherit ['super] b as b
  inherit ['super] c as c
  method supers =  `d (self :> _ d) :: b#supers @ c#supers
  method d = ()
end

class ['super] e = object (self : 'self)
  inherit ['super] d as d
  method supers = `e (self :> _ e) :: d#supers
  method e = ()
end

let d = (new d :> _ a)
let a : _ a = cast (function `a o -> Some o | _ -> None) d
let b : _ b = cast (function `b o -> Some o | _ -> None) d
let c : _ c = cast (function `c o -> Some o | _ -> None) d
let d : _ d = cast (function `d o -> Some o | _ -> None) d

let c = new c
let error : _ b = cast (function `b o -> Some o | _ -> None) c


  parent reply	other threads:[~2008-02-27 13:30 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-02-26 11:35 Berke Durak
2008-02-26 12:14 ` [Caml-list] " Richard Jones
2008-02-26 14:28   ` Berke Durak
2008-02-26 14:48     ` Richard Jones
2008-02-26 14:59       ` Berke Durak
2008-02-27 13:26   ` Tiphaine.Turpin [this message]
2008-02-29 10:36     ` Berke Durak
2008-02-29 12:23       ` Tiphaine.Turpin
2008-02-26 12:48 ` ketti
2008-02-26 13:10 ` Berke Durak
2008-02-26 15:07 ` Dirk Thierbach
2008-02-26 16:25   ` Berke Durak
2008-02-27  7:37     ` Dirk Thierbach
2008-02-27 10:26       ` Berke Durak

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=47C564EA.4060902@free.fr \
    --to=tiphaine.turpin@free.fr \
    --cc=caml-list@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;
as well as URLs for NNTP newsgroup(s).