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 MAA10855; Tue, 17 Dec 2002 12:37:26 +0100 (MET) 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 MAA10851 for ; Tue, 17 Dec 2002 12:37:25 +0100 (MET) Received: from isis.lip6.fr (isis.lip6.fr [132.227.60.2]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id gBHBbOn13099 for ; Tue, 17 Dec 2002 12:37:24 +0100 (MET) Received: from spi.lip6.fr (IDENT:root@spi.lip6.fr [132.227.83.55]) by isis.lip6.fr (8.12.4/jtpda-5.4+victor) with ESMTP id gBHBbN67010311 ; Tue, 17 Dec 2002 12:37:23 +0100 X-pt: isis.lip6.fr Received: from localhost.localdomain (etna.lip6.fr [132.227.83.203]) by spi.lip6.fr (8.8.7/jtpda-5.2) with SMTP id NAA00971 ; Tue, 17 Dec 2002 13:37:26 +0100 Date: Tue, 17 Dec 2002 12:37:21 +0100 From: Virgile Prevosto To: Nobuyuki Tomizawa Cc: caml-list@inria.fr Subject: Re: [Caml-list] Question on typing of class/object and optional argument. Message-Id: <20021217123721.43010c7d.virgile.prevosto@lip6.fr> In-Reply-To: <20021216.011851.730550325.nobuyuki@mua.biglobe.ne.jp> References: <20021216.011851.730550325.nobuyuki@mua.biglobe.ne.jp> Organization: LIP6 X-Mailer: Sylpheed version 0.8.6claws (GTK+ 1.2.10; ) Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Scanned-By: isis.lip6.fr Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Hello, Nobuyuki Tomizawa a écrit: > ------------------------------------------------------------ > class foo s = object > val str : string = s > method to_string ?(opt = "" ) () = opt ^ s > end;; > > let l = [new foo "a"; new foo "b"; new foo "c" ];; > > List.iter (fun e -> print_endline (e#to_string ())) l;; > > File "test.ml", line 8, characters 52-53: > This expression has type foo list but is here used with type > < to_string : unit -> string > list > Type foo = < to_string : ?opt:string -> unit -> string > > is not compatible with type < to_string : unit -> string > > ------------------------------------------------------------ This is quite normal: since the type of e is not constrained, ocaml has inferred the most general one from the body of the function: e must be an object with a method 'to_string' of type unit -> string . The problem comes from the fact that optional arguments and type inference do not work very well together (cf http://pauillac.inria.fr/ocaml/htmlman/manual006.html section 4.1.2). As suggested in the manual, the best solution might be to add a type annotation in the function above: List.iter (fun (e:foo) -> print_endline (e#to_string ())) l;; or List.iter (fun (e:#foo) -> print_endline (e#to_string ())) l;; if you intend to use subclasses of foo > > In contrast, I did not get the above message if I rewrote the program > into module style like: > module Bar : BAR = struct > type t = string > let create t = t > let to_string ?(opt = "") t = opt ^ t > end;; > > let m = [Bar.create "a"; Bar.create "b"; Bar.create "c" ];; > > List.iter (fun e -> print_endline (Bar.to_string e)) m;; Here, Bar.to_string has a perfectly defined type (namely ?opt:string -> Bar.t -> string), and optionnal arguments are treated inside the function. e itself is not involved in the process: it should only be of type Bar.t, and m is indeed a Bar.t list, so that everything works fine. In the object version, e is an object whose method to_string must have a certain type in which optionnal arguments are not necessarily taken into account. I'm afraid I'm not very clear here, but I'm not very familiar with labels, sorry... -- E tutto per oggi, a la prossima volta Virgile ------------------- 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