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=0.0 required=5.0 tests=AWL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from discorde.inria.fr (discorde.inria.fr [192.93.2.38]) by yquem.inria.fr (Postfix) with ESMTP id 74D76BC0A for ; Mon, 4 Jun 2007 18:45:15 +0200 (CEST) Received: from mail.cs.rice.edu (mail.cs.rice.edu [128.42.1.31]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id l54GjDjN024657 for ; Mon, 4 Jun 2007 18:45:15 +0200 Received: from mail.cs.rice.edu (localhost.localdomain [127.0.0.1]) by mail.cs.rice.edu (Postfix) with ESMTP id AAF282C2A65 for ; Mon, 4 Jun 2007 11:45:12 -0500 (CDT) X-Virus-Scanned: by amavis-2.4.0 at mail.cs.rice.edu Received: from mail.cs.rice.edu ([127.0.0.1]) by mail.cs.rice.edu (mail.cs.rice.edu [127.0.0.1]) (amavisd-new, port 10024) with LMTP id b+41fNmFtaou for ; Mon, 4 Jun 2007 11:45:04 -0500 (CDT) Received: from [10.249.109.2] (dunwlessnat.rice.edu [128.42.64.251]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client certificate requested) by mail.cs.rice.edu (Postfix) with ESMTP id D96942C2A8E for ; Mon, 4 Jun 2007 11:45:04 -0500 (CDT) Mime-Version: 1.0 (Apple Message framework v750) Content-Transfer-Encoding: 7bit Message-Id: <0EE1C559-A93C-4E66-AC70-8ADD1C21DDF8@rice.edu> Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed To: caml-list@yquem.inria.fr From: Raj B Subject: OCaml object types vs record types Date: Mon, 4 Jun 2007 11:45:02 -0500 X-Mailer: Apple Mail (2.750) X-Miltered: at discorde with ID 46644199.001 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; ocaml:01 subtyping:01 mutable:01 ocaml:01 mutable:01 subtyping:01 coercion:01 coercion:01 undefined:01 int:01 int:01 define:01 define:01 structures:02 objects:02 Hi there I'm writing an application where I'd modeling objects as record types. However, I'd like to have a subtyping relation between them along with (occasional) coercion. For example, type obj = {name : string} type specialobj = {name: string; mutable value: int;...} and so on... Obviously, records obviously don't allow me to do this. In fact, I can't even define two record types with the same field 'name'. I have to define the 'largest' possible type i.e. specialobj and just leave fields undefined (default value) when I don't need them. This is fine, except that it eats up tons of memory because my records are quite large and slows things down. On profiling, I find that most of my time goes in the OCaml function 'caml_dup', which, I believe, copies structures. I am very concerned about speed in my application. I'm not sure I need the entire machinery of classes (since, I understand that it can be pretty slow too?) and all that goes with it, so I was wondering if using object types might serve the purpose. type obj = type specialobj = Unfortunately, it doesn't seem possible to define 'mutable' fields in the same way as records. I'd have to define methods to get and set values, so it seems like there's very little difference from class definitions. Please correct me if I'm wrong. Is there any way to get what I want (records with subtyping/coercion behavior) without using classes? In case I do end up using classes, what kind of performance penalty am I looking at? Thanks! Raj