From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id EAA31585; Sat, 21 Aug 2004 04:45:05 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id EAA32557 for ; Sat, 21 Aug 2004 04:45:03 +0200 (MET DST) Received: from smtp1.adl2.internode.on.net (smtp1.adl2.internode.on.net [203.16.214.181]) by nez-perce.inria.fr (8.12.10/8.12.10) with ESMTP id i7L2j0mL027060 for ; Sat, 21 Aug 2004 04:45:02 +0200 Received: from [192.168.1.200] (ppp211-206.lns2.syd3.internode.on.net [203.122.211.206]) by smtp1.adl2.internode.on.net (8.12.9/8.12.9) with ESMTP id i7L2ir4Y011275; Sat, 21 Aug 2004 12:14:54 +0930 (CST) Subject: Re: [Caml-list] Programming with classes From: skaller Reply-To: skaller@users.sourceforge.net To: Jean-Baptiste Rouquier Cc: caml-list In-Reply-To: <1093053438.4126abfecddbe@mouette.ens-lyon.fr> References: <1093051727.29139.1503.camel@pelican.wigram> <1093053438.4126abfecddbe@mouette.ens-lyon.fr> Content-Type: text/plain Message-Id: <1093056292.29139.1569.camel@pelican.wigram> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.2.2 (1.2.2-4) Date: 21 Aug 2004 12:44:53 +1000 Content-Transfer-Encoding: 7bit X-Miltered: at nez-perce with ID 4126B72C.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 sourceforge:01 2004:99 quoting:01 invariants:01 list':01 'make:01 abstraction:01 fragile:01 9660:01 glebe:01 unsafe:01 ocaml:01 ocaml:01 nsw:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Sat, 2004-08-21 at 11:57, Jean-Baptiste Rouquier wrote: > Quoting skaller: > >There is a further step: constructors. Never export > >class constructors. They must be wrapped in a normal > >Ocaml function which returns an abstract class type. > > I would be glad to follow this rule, since I'm experiencing aesthetical problems > with constructors (see below). But could you develop a bit on this ? Well, I will give an 'aesthetic' argument then :) Ocaml only allows a single constructor. Typically you'd like to make it just establish the data of the class -- it might not even bother checking invariants. However, from a usage viewpoint, you often need multiple constructors -- for example 'empty list' and 'list of one element' are good starting points for a list like object. Or you might have 'make_rational(p,q)' and 'unsafe_make_rational(p,q)' the latter assuming that p,q are relatively prime and q>0, and the former checking q<>0 and then reducing p,q to be relatively prime -- obviously this function is expensive and is used when the client passes p,q -- but if you're copying a Rational_Number object there is no need since the invariant is already assured. But one can go further and say that, well, anything which creates an object is a constructor. For example if you concantenate two 'list like objects' to get 'list like object' is not that function a constructor? It is, after all, making a new object. So it seems sensible to 'abstract' the notion of 'user accessible constructor' away from the physical constructor and make the physical type constructor as brain dead and efficient as possible, the price being it is unsafe: we protect against that by restricting access to it. > My problem is that I'm exporting many class constructors (if you explain me why > I shouldnt, the next version probably won't) There is a trade off, obviously: abstraction costs. One argument against exporting actual constructors is that it makes your code fragile. It is hard to change the representation of a type, when the constructor often needs representation dependent data as arguments. -- John Skaller, mailto:skaller@users.sf.net voice: 061-2-9660-0850, snail: PO BOX 401 Glebe NSW 2037 Australia Checkout the Felix programming language http://felix.sf.net ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners