caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Olivier Andrieu <andrieu@ijm.jussieu.fr>
To: Nobuyuki Tomizawa <n-tomizawa@mua.biglobe.ne.jp>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Question on typing of class/object and optional argument.
Date: Tue, 17 Dec 2002 14:22:03 +0100	[thread overview]
Message-ID: <15871.9467.357724.571888@akasha.ijm.jussieu.fr> (raw)
In-Reply-To: <20021217123721.43010c7d.virgile.prevosto@lip6.fr>

 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


      reply	other threads:[~2002-12-17 13:22 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-12-15 16:18 Nobuyuki Tomizawa
2002-12-17 11:37 ` Virgile Prevosto
2002-12-17 13:22   ` Olivier Andrieu [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=15871.9467.357724.571888@akasha.ijm.jussieu.fr \
    --to=andrieu@ijm.jussieu.fr \
    --cc=caml-list@inria.fr \
    --cc=n-tomizawa@mua.biglobe.ne.jp \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).