<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE message PUBLIC
  "-//MLarc//DTD MLarc output files//EN"
  "../../mlarc.dtd"[
  <!ATTLIST message
    listname CDATA #REQUIRED
    title CDATA #REQUIRED
  >
]>

  <?xml-stylesheet href="../../mlarc.xsl" type="text/xsl"?>


<message 
  url="2002/12/140c8bb5b9f6c581f3755dc0851c4a37"
  from="Olivier Andrieu &lt;andrieu@i...&gt;"
  author="Olivier Andrieu"
  date="2002-12-17T13:22:12"
  subject="Re: [Caml-list] Question on typing of class/object and optional argument."
  prev="2002/12/b0663d289638f7afc876e9898cb3915a"
  next="2002/12/16a7bcfb8614e7c6474384514cbd3fdb"
  prev-in-thread="2002/12/b0663d289638f7afc876e9898cb3915a"
  prev-thread="2002/12/5688e361e75097ac092c3d5bff9beae5"
  next-thread="2002/12/69ebad4a16ce944d2f01e9878f67739b"
  root="../../"
  period="month"
  listname="caml-list"
  title="Archives of the Caml mailing list">

<thread subject="[Caml-list] Question on typing of class/object and optional argument. ">
<msg 
  url="2002/12/c1c05f314ad9a12a76fe27ff77f36cd8"
  from="Nobuyuki Tomizawa &lt;n-tomizawa@m...&gt;"
  author="Nobuyuki Tomizawa"
  date="2002-12-15T16:12:42"
  subject="[Caml-list] Question on typing of class/object and optional argument. ">
<msg 
  url="2002/12/b0663d289638f7afc876e9898cb3915a"
  from="Virgile Prevosto &lt;virgile.prevosto@l...&gt;"
  author="Virgile Prevosto"
  date="2002-12-17T11:37:26"
  subject="Re: [Caml-list] Question on typing of class/object and optional argument.">
<msg 
  url="2002/12/140c8bb5b9f6c581f3755dc0851c4a37"
  from="Olivier Andrieu &lt;andrieu@i...&gt;"
  author="Olivier Andrieu"
  date="2002-12-17T13:22:12"
  subject="Re: [Caml-list] Question on typing of class/object and optional argument.">
</msg>
</msg>
</msg>
</thread>

<contents>
 Virgile Prevosto [Tuesday 17 December 2002] :
 &gt;
 &gt; Hello,
 &gt; Nobuyuki Tomizawa a écrit:
 &gt; 
 &gt; &gt; ------------------------------------------------------------
 &gt; &gt; class foo s = object
 &gt; &gt;   val str : string = s
 &gt; &gt;   method to_string ?(opt = "" ) () = opt ^ s
 &gt; &gt; end;;
 &gt; &gt; 
 &gt; &gt; let l = [new foo "a"; new foo "b"; new foo "c" ];;
 &gt; &gt; 
 &gt; &gt; List.iter (fun e -&gt; print_endline (e#to_string ())) l;;
 &gt; &gt; 
 &gt; &gt; File "test.ml", line 8, characters 52-53:
 &gt; &gt; This expression has type foo list but is here used with type
 &gt; &gt;   &lt; to_string : unit -&gt; string &gt; list
 &gt; &gt; Type foo = &lt; to_string : ?opt:string -&gt; unit -&gt; string &gt;
 &gt; &gt; is not compatible with type &lt; to_string : unit -&gt; string &gt; 
 &gt; &gt; ------------------------------------------------------------
 &gt; 
 &gt; This is quite normal: since the type of e is not constrained, ocaml
 &gt; has inferred the most general one from the body of the function: e
 &gt; must be an object with a method 'to_string' of type unit -&gt; string
 &gt; . The problem comes from the fact that optional arguments and type
 &gt; inference do not work very well together (cf
 &gt; http://pauillac.inria.fr/ocaml/htmlman/manual006.html section
 &gt; 4.1.2).
 &gt; As suggested in the manual, the best solution might be to add a
 &gt; type annotation in the function above:
 &gt; 
 &gt; List.iter (fun (e:foo) -&gt; print_endline (e#to_string ())) l;;
 &gt; or
 &gt; List.iter (fun (e:#foo) -&gt; print_endline (e#to_string ())) l;;
 &gt; if you intend to use subclasses of foo

Alternatively, one can use an "unwrapped" argument :

# List.iter (fun e -&gt; 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

</contents>

</message>

