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
[Caml-list] Alternate constructors for a class
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2003-09-02 (13:45)
From: Richard Jones <rich@a...>
Subject: [Caml-list] Alternate constructors for a class
I have a class defined currently like this:

  class view ?width ?height ?packing ?show ?(title = "untitled") () =
    (* ... *)

I want to provide an alternate way of constructing this class, in this
case by unmarshalling it from a channel (using the Marshall module).
Callers should be able to construct a view either using the explicit
parameters, or by doing:

  let chan = open_in_bin "serialized_class" in
  let view = new view ~chan in
  (* ... *)

Now in the case above I guess I could add an extra optional ?chan
argument, but that seems a bit ugly because it doesn't prevent users
from supplying incorrectly both ~chan and ~title, for example.

Is there an easy way to provide two different constructors for a

Other languages do allow different sets of arguments to different
constructors for the same class quite naturally.


Richard Jones.
Merjis Ltd. - all your business data are belong to you.
"I wish more software used text based configuration files!"
 -- A Windows NT user, quoted on Slashdot.

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: