caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Question on typing of class/object and optional argument.
@ 2002-12-15 16:18 Nobuyuki Tomizawa
  2002-12-17 11:37 ` Virgile Prevosto
  0 siblings, 1 reply; 3+ messages in thread
From: Nobuyuki Tomizawa @ 2002-12-15 16:18 UTC (permalink / raw)
  To: caml-list

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 -> 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
  < to_string : unit -> string > list
Type foo = < to_string : ?opt:string -> unit -> string >
is not compatible with type < to_string : unit -> string > 
------------------------------------------------------------

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 -> t
  val to_string : ?opt:string -> t -> 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 -> 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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Question on typing of class/object and optional argument.
  2002-12-15 16:18 [Caml-list] Question on typing of class/object and optional argument Nobuyuki Tomizawa
@ 2002-12-17 11:37 ` Virgile Prevosto
  2002-12-17 13:22   ` Olivier Andrieu
  0 siblings, 1 reply; 3+ messages in thread
From: Virgile Prevosto @ 2002-12-17 11:37 UTC (permalink / raw)
  To: Nobuyuki Tomizawa; +Cc: caml-list

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
> 
> In contrast, I did not get the above message if I rewrote the program
> into module style like:
> 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 -> print_endline (Bar.to_string e)) m;;

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Question on typing of class/object and optional argument.
  2002-12-17 11:37 ` Virgile Prevosto
@ 2002-12-17 13:22   ` Olivier Andrieu
  0 siblings, 0 replies; 3+ messages in thread
From: Olivier Andrieu @ 2002-12-17 13:22 UTC (permalink / raw)
  To: Nobuyuki Tomizawa; +Cc: caml-list

 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


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2002-12-17 13:22 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-12-15 16:18 [Caml-list] Question on typing of class/object and optional argument Nobuyuki Tomizawa
2002-12-17 11:37 ` Virgile Prevosto
2002-12-17 13:22   ` Olivier Andrieu

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).