Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Instruction selection in the OCaml compiler: Modules or classes?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2007-02-22 (17:21)
From: Tom <tom.primozic@g...>
Subject: Re: [Caml-list] Instruction selection in the OCaml compiler: Modules or classes?
Classes in OCaml are indeed slow, but that is because they are incredibly
different from modules. A class has fields, that is, meathods, that are
"named" and searched by comparing hashes. Modules also have fields,
functions and values, but they are named only as far as the programmer in
concerned - in compiled code, they are indexed (by an integer), and
actually, the native compiler can actually optimise even that (so only a
plain function call remains). In contrast, everytime you call a method of an
object, it is searched in the method list of that class (well, I think there
is some caching too).

But this "slowness" is expected - objects are also more adaptable than
modules. If you have a function
    # let f o = o#some_method ();;
    val f : < some_method : unit -> 'a; .. > -> 'a = <fun>
you can pass to it any object with method "some_method" of type unit -> 'a.
That's called structural subtyping.

- Tom