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 OAA13906; Tue, 17 Dec 2002 14:22:12 +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 OAA13902 for ; Tue, 17 Dec 2002 14:22:10 +0100 (MET) Received: from shiva.jussieu.fr (shiva.jussieu.fr [134.157.0.129]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id gBHDMAn15702 for ; Tue, 17 Dec 2002 14:22:10 +0100 (MET) Received: from akasha.ijm.jussieu.fr (akasha.ijm.jussieu.fr [134.157.173.57]) by shiva.jussieu.fr (8.12.5/jtpda-5.4) with ESMTP id gBHDM8jC046607 ; Tue, 17 Dec 2002 14:22:09 +0100 (CET) Received: by akasha.ijm.jussieu.fr (Postfix, from userid 11004) id A236C3C54A; Tue, 17 Dec 2002 14:22:03 +0100 (CET) From: Olivier Andrieu MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Message-ID: <15871.9467.357724.571888@akasha.ijm.jussieu.fr> Date: Tue, 17 Dec 2002 14:22:03 +0100 To: Nobuyuki Tomizawa Cc: caml-list@inria.fr Subject: Re: [Caml-list] Question on typing of class/object and optional argument. In-Reply-To: <20021217123721.43010c7d.virgile.prevosto@lip6.fr> References: <20021216.011851.730550325.nobuyuki@mua.biglobe.ne.jp> <20021217123721.43010c7d.virgile.prevosto@lip6.fr> X-Mailer: VM 7.07 under Emacs 21.2.1 X-Antivirus: scanned by sophie at shiva.jussieu.fr Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Virgile Prevosto [Tuesday 17 December 2002] : > > 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 Alternatively, one can use an "unwrapped" argument : # List.iter (fun e -> print_endline (e#to_string ?opt:None ())) l;; a b c - : unit = () -- Olivier ------------------- 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