From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id 62228BB91 for ; Sun, 9 Jan 2005 19:45:14 +0100 (CET) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j09IjDgY002619 for ; Sun, 9 Jan 2005 19:45:13 +0100 Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id TAA22810 for ; Sun, 9 Jan 2005 19:45:13 +0100 (MET) From: padiolea@irisa.fr Received: from smtp.irisa.fr (smtp.irisa.fr [131.254.254.26]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id j09IjDrl004796 for ; Sun, 9 Jan 2005 19:45:13 +0100 Received: from localhost (localhost.localdomain [127.0.0.1]) by localhost.irisa.fr (Postfix) with ESMTP id E1B5FFA9B; Sun, 9 Jan 2005 19:45:12 +0100 (CET) Received: from smtp.irisa.fr ([131.254.254.26]) by localhost (meli.irisa.fr [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 06092-09; Sun, 9 Jan 2005 19:45:12 +0100 (CET) Received: from mail.irisa.fr (melo.irisa.fr [131.254.254.28]) by smtp.irisa.fr (Postfix) with ESMTP id 22FD3FA9A; Sun, 9 Jan 2005 19:45:12 +0100 (CET) Received: from 80.8.1.98 (SquirrelMail authenticated user padiolea); by mail.irisa.fr with HTTP; Sun, 9 Jan 2005 19:45:12 +0100 (CET) Message-ID: <1082.80.8.1.98.1105296312.squirrel@80.8.1.98> In-Reply-To: References: <20050109131928.GA1759@wafthrudnir> Date: Sun, 9 Jan 2005 19:45:12 +0100 (CET) Subject: Re: [Caml-list] generic functions To: "Brian Hurt" Cc: wiedergaenger@fastmail.fm, caml-list@inria.fr User-Agent: SquirrelMail/1.4.3a X-Mailer: SquirrelMail/1.4.3a MIME-Version: 1.0 Content-Type: text/plain;charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Priority: 3 (Normal) Importance: Normal X-Virus-Scanned: by amavisd-new at irisa.fr X-Miltered: at nez-perce with ID 41E17BB9.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at concorde with ID 41E17BB9.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; irisa:01 caml-list:01 wrote:01 ocaml:01 clos:01 ocaml:01 foo:01 foo:01 redefined:01 pointless:01 htmlman:01 defintion:01 overloading:01 overloading:01 polymorphism:01 X-Spam-Checker-Version: SpamAssassin 3.0.0 (2004-09-13) on yquem.inria.fr X-Spam-Status: No, score=0.2 required=5.0 tests=NO_REAL_NAME autolearn=disabled version=3.0.0 X-Spam-Level: > On Sun, 9 Jan 2005 wiedergaenger@fastmail.fm wrote: > >> I just got from LISP to OCaml, and wondered if there is an equivalent of >> generic functions from LISP (CLOS) in OCaml. In the Common Lisp Object >> System methods don't belong to certain objects/classes. They are just >> function specializing on the argument types. So basically I want to >> write something like: >> >> let foo (x : int) = x*x;; >> let foo (x : float) = x*.x;; >> >> This, obviously, will not work since foo is just redefined by the second >> statement. One would think, that having methods not being belonging to >> objects/classes, is rather pointless. Well 95% of the time, there is no >> necessity for that. But in the other 5%, it is really helpful. >> > ... > 1) Just use different functions. Do: ... > 2) Use variant types: ... > 3) Use modules: ... I would add 4) use objects. see http://caml.inria.fr/ocaml/htmlman/manual005.html and define a class ofloat, and oint (for object float and objects int) and define let foo x = x#mul x Note that foo does not have to belong to the classes. It can be defined outside of the class defintion. Of course if your definition of foo would have been let foo (x : int) = x+x;; (* * have been replaced by + *) let foo (x : float) = x*.x;; then, it does not work anymore. > > With the exception of certain artificial contests (Paul Graham) I've never > met a real world problem that needed overloading, or even benefitted > signifigantly from overloading that didn't benefit just as much or more > from one of the solutions above. I guess that's because you never been involved in real world problems. Overloading, which is adhoc polymorphism is as useful as parametric polymorphism. Ask the haskell folks if they would accept to forget about type classes. Of course functors can emulate some of the advantages of overloading, but not totally. Objects are better for this. > > Brian > > > _______________________________________________ > Caml-list mailing list. Subscription management: > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list > Archives: http://caml.inria.fr > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs >