caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Raj B <rajb@rice.edu>
To: caml-list@yquem.inria.fr
Subject: OCaml object types vs record types
Date: Mon, 4 Jun 2007 11:45:02 -0500	[thread overview]
Message-ID: <0EE1C559-A93C-4E66-AC70-8ADD1C21DDF8@rice.edu> (raw)

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 = <name : string>
type specialobj = <name: string; mutable value: int;...>

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


             reply	other threads:[~2007-06-04 16:45 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-06-04 16:45 Raj B [this message]
2007-06-04 19:55 ` [Caml-list] " Pierre-Evariste Dagand
2007-06-04 20:13 ` Nicolas Pouillard
2007-06-04 22:06   ` Lukasz Stafiniak

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=0EE1C559-A93C-4E66-AC70-8ADD1C21DDF8@rice.edu \
    --to=rajb@rice.edu \
    --cc=caml-list@yquem.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).