From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: * X-Spam-Status: No, score=1.6 required=5.0 tests=AWL,DNS_FROM_RFC_POST, SPF_NEUTRAL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id C15EBBB84 for ; Mon, 14 Jul 2008 03:48:01 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgcFADBNekhKfS4dYWdsb2JhbACRajkaBBwFkWiELA X-IronPort-AV: E=Sophos;i="4.30,355,1212357600"; d="scan'208";a="27295753" Received: from yw-out-2324.google.com ([74.125.46.29]) by mail4-smtp-sop.national.inria.fr with ESMTP; 14 Jul 2008 03:48:01 +0200 Received: by yw-out-2324.google.com with SMTP id 5so2069520ywb.3 for ; Sun, 13 Jul 2008 18:47:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:from:reply-to:to:subject:date :user-agent:cc:references:in-reply-to:content-type :content-transfer-encoding:content-disposition:message-id; bh=Si3E76Bo3kcyyn58/1ZqwWS7ofx6l5whjj0F4Mp7vnc=; b=gToY8Qh2Uer9GzbKTDZW5Xy6x+voPovg+CEvppp9pBn0Ad5NpAJJFqVsX7ADT+x6Bo 4bTfIdFgx1ndMWU6ZD96QZ/JKLD/tfN6IEMOoKXdT3xYwdIfkJZzOncXYJlzNQgjzZz0 BOVMo7LdFrz7E0uQCo6Xxc3lOIGcK+AZgxbj8= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:reply-to:to:subject:date:user-agent:cc:references:in-reply-to :content-type:content-transfer-encoding:content-disposition :message-id; b=aCJaWgWWbKgLLX2HJnT9IXjmoB5zi50+JXpBh+YFBPN/u4+bTlTysNYhyMhiztSt6d Dt5BykzYvdk591dyye8t6g0eN32lrE8J5q2690kj8khnj1CP5qymVkXLq8J+o7LB4erG h0HddfghXnI6WtLHKnjJRRGeNfxm58+dbKmog= Received: by 10.150.212.12 with SMTP id k12mr7663569ybg.228.1216000077700; Sun, 13 Jul 2008 18:47:57 -0700 (PDT) Received: from ?192.168.0.102? ( [24.99.180.210]) by mx.google.com with ESMTPS id 6sm5230850ywp.3.2008.07.13.18.47.56 (version=TLSv1/SSLv3 cipher=RC4-MD5); Sun, 13 Jul 2008 18:47:56 -0700 (PDT) From: Peng Zang Reply-To: peng.zang@gmail.com To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Why can't immediate objects be extended? Date: Sun, 13 Jul 2008 21:47:51 -0400 User-Agent: KMail/1.9.7 Cc: Richard Jones , caml-list@inria.fr References: <20080712141200.GA8634@annexia.org> In-Reply-To: <20080712141200.GA8634@annexia.org> Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200807132147.55560.peng.zang@gmail.com> X-Spam: no; 0.00; hash:01 syntax:01 hash:01 methodname:01 methodname:01 obj':01 dup:01 obj':01 foo:01 foo:01 foobar:01 foobar:01 val:01 val:01 peng:98 -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Saturday 12 July 2008 10:12:00 am Richard Jones wrote: > I'm wondering if there's a reason why one cannot inherit from > immediate objects? (See description at: > http://caml.inria.fr/pub/docs/manual-ocaml/manual005.html#ss:immediate-obje >cts) Is it just a syntax problem because the 'inherit' keyword currently > needs to take a class type, or is there a deeper reason? I have no idea, however, using some black magic we can accomplish exactly this: - ----------- snip of temp.ml ------------- (** ported from hash.c **) let caml_hash_variant name = let acc = ref 0 in let len = String.length name in for i = 0 to len - 1 do acc := (223 * !acc + Char.code (String.get name i)) land 0x7FFFFFFF; done; !acc ;; (** the return type is needs casting **) let addmethod obj methodname methodfun = let methodsuite = Obj.field obj 0 in let newms = let nummethods : int = Obj.magic (Obj.field methodsuite 0) in let size = Obj.size methodsuite in let arr = Array.create (size+2) 0 in arr.(0) <- nummethods + 1; for i = 1 to size - 1 do arr.(i) <- Obj.magic (Obj.field methodsuite i); done; arr.(size) <- methodfun; arr.(size+1) <- caml_hash_variant methodname; arr in let obj' = Obj.dup obj in Obj.set_field obj' 0 (Obj.magic newms); obj' ;; let addmethod = Obj.magic addmethod let addmethod : (< .. > as 'a) -> string -> ('a -> 'b) -> < .. > = addmethod;; let foo = object method foo = "foo" end;; let foobar : = addmethod foo "bar" (fun _ -> "bar");; foo#foo;; foobar#foo;; foobar#bar;; - ----------- end snip ---------- Objective Caml version 3.09.3 # #use "temp.ml";; val caml_hash_variant : string -> int = val addmethod : Obj.t -> string -> int -> Obj.t = val addmethod : 'a = val addmethod : (< .. > as 'a) -> string -> ('a -> 'b) -> < .. > = val foo : < foo : string > = val foobar : < bar : string; foo : string > = - - : string = "foo" - - : string = "foo" - - : string = "bar" # Not pretty, but it works, Peng -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.7 (GNU/Linux) iD8DBQFIerBLfIRcEFL/JewRAnTpAJ9LdD09DsVvgJHemCf3ROzGqH+dQwCfRAoX 7sOvInXyvq8jDfF+ucZhzic= =V3Ki -----END PGP SIGNATURE-----