<?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/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."
  prev="2002/12/d8ad31ee763c42940b410290b1d27578"
  next="2002/12/140c8bb5b9f6c581f3755dc0851c4a37"
  prev-in-thread="2002/12/c1c05f314ad9a12a76fe27ff77f36cd8"
  next-in-thread="2002/12/140c8bb5b9f6c581f3755dc0851c4a37"
  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>
Hello,
Nobuyuki Tomizawa a écrit:

&gt; ------------------------------------------------------------
&gt; class foo s = object
&gt;   val str : string = s
&gt;   method to_string ?(opt = "" ) () = opt ^ s
&gt; end;;
&gt; 
&gt; let l = [new foo "a"; new foo "b"; new foo "c" ];;
&gt; 
&gt; List.iter (fun e -&gt; print_endline (e#to_string ())) l;;
&gt; 
&gt; File "test.ml", line 8, characters 52-53:
&gt; This expression has type foo list but is here used with type
&gt;   &lt; to_string : unit -&gt; string &gt; list
&gt; Type foo = &lt; to_string : ?opt:string -&gt; unit -&gt; string &gt;
&gt; is not compatible with type &lt; to_string : unit -&gt; string &gt; 
&gt; ------------------------------------------------------------

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 -&gt; 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) -&gt; print_endline (e#to_string ())) l;;
or
List.iter (fun (e:#foo) -&gt; print_endline (e#to_string ())) l;;
if you intend to use subclasses of foo
&gt; 
&gt; In contrast, I did not get the above message if I rewrote the program
&gt; into module style like:
&gt; module Bar : BAR = struct
&gt;   type t = string
&gt;   let create t = t
&gt;   let to_string ?(opt = "") t = opt ^ t
&gt; end;;
&gt; 
&gt; let m = [Bar.create "a"; Bar.create "b"; Bar.create "c" ];;
&gt; 
&gt; List.iter (fun e -&gt; print_endline (Bar.to_string e)) m;;

Here, Bar.to_string has a perfectly defined type 
(namely ?opt:string -&gt; Bar.t -&gt; 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

</contents>

</message>

