<?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/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. "
  prev="2002/12/dcef1473d1a3389ba1585d4e2d4bb51a"
  next="2002/12/c6cf5d6a698ddeb08791e29a39e73382"
  next-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>
Dear list:

I have a question on class/object with optional arguments method.

I could not compile following program:
------------------------------------------------------------
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 -&gt; print_endline (e#to_string ())) l;;
------------------------------------------------------------

with folloing message.

------------------------------------------------------------
File "test.ml", line 8, characters 52-53:
This expression has type foo list but is here used with type
  &lt; to_string : unit -&gt; string &gt; list
Type foo = &lt; to_string : ?opt:string -&gt; unit -&gt; string &gt;
is not compatible with type &lt; to_string : unit -&gt; string &gt; 
------------------------------------------------------------

In contrast, I did not get the above message if I rewrote the program
into module style like:

------------------------------------------------------------
module type BAR = sig
  type t
  val create : string -&gt; t
  val to_string : ?opt:string -&gt; t -&gt; string
end;;

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 -&gt; print_endline (Bar.to_string e)) m;;
------------------------------------------------------------

I can not understand how different these two versions are in Ocaml's
typing systems.

Colud you please tell me the point and what should I know to
understand well about such kind of typing issues?

Thanks in advance.

Nobuyuki Tomizawa
-------------------
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>

