caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: William Chesters <williamc@dai.ed.ac.uk>
To: "Mark Engelberg" <mark.engelberg@bigfoot.com>
Cc: <caml-list@inria.fr>
Subject: Classes AND Modules?  What's the point?
Date: Wed, 3 Nov 1999 23:13:07 GMT	[thread overview]
Message-ID: <199911032313.XAA17986@toy.william.bogus> (raw)
In-Reply-To: <00fb01bf2447$9ef382a0$03dedede@walldata.com>

Mark Engelberg writes:
 > The thing that confuses me the most about OCaml is that there is a huge
 > overlap between the functionality of classes and modules, with a couple of
 > subtle differences.

   Interestingly, the big difference is that modules are not "first
class values".  You can't pass the whole thing around, along with its
dispatch table, like you can an object.  You can't write

	module type Shape = sig
	  val area: unit -> float
	end

	module Square (Init: sig val side: float end) = struct
	  let side = Init.side
	  let area () = side *. side
	end

	let foo M = M.area ()     (* this isn't allowed *)

	let _ =
	  let module It = Square (struct let side = 50. end)
	  in
	  foo It                  (* nor is this *)

So although you can create modules on the fly with "let module", and
pass their _members_ around, you can't really use the instantiations
conveniently as objects.  There is no way to use modules per se to
achieve class-style polymorphism.

   I have often thought that you could get something very like the
ocaml class system, with similar semantics and implementational
issues, by this kind of route.

 > The problem is that because modules are slightly more powerful, it appears
 > that the entire standard library is implemented as modules, not classes,
 > despite the fact that this is supposed to be Object-oriented Caml!

   IIRC OLabl ships with object versions of some of the libraries.

   Don't forget that object a method invocation is a little slower
than a module function call---I measured fetches from a Hashtbl,
indirected through an object wrapper, about 10% slower than fetches
indirected through a module.  Anyway, object-using programs don't
always necessarily work out prettier than module-based ones.




  reply	other threads:[~1999-11-04 18:50 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-11-01  9:01 Mark Engelberg
1999-11-03 23:13 ` William Chesters [this message]
1999-11-05 10:13 ` Xavier Leroy
1999-11-05 18:49 ` Francisco Valverde Albacete
1999-11-07 23:31   ` Gerd Stolpmann

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=199911032313.XAA17986@toy.william.bogus \
    --to=williamc@dai.ed.ac.uk \
    --cc=caml-list@inria.fr \
    --cc=mark.engelberg@bigfoot.com \
    /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).