From: "Sylvain BOULM'E" <Sylvain.Boulme@lip6.fr>
To: "Mattias Waldau" <mattias.waldau@tacton.se>
Cc: caml-list@inria.fr
Subject: Re: Why can't I use val mover : < move : int -> unit; .. > list -> unit ?
Date: Thu, 11 Jan 2001 14:09:21 +0100 [thread overview]
Message-ID: <200101111309.OAA02994@baltazard.lip6.fr> (raw)
In-Reply-To: Your message of "Thu, 11 Jan 2001 10:07:33 +0100." <HDEEKOMJILGEIHIMAPCDGEFPDNAA.mattias.waldau@tacton.se>
> I understand the principle that the compiler must be sure that everywhere I
> use the list, the elements will be of the right type. In this case each
> element must have the method move.
>
> However, it seems to me that in this case the compiler has already done the
> complex part, i.e. realizing that if all objects have a move-method, then
> everything is ok. So I don't see the point that I have to coerce explicitily
> when putting elements into the list.
>
Such a coercion is not always possible: forget the "unnecessary" methods has not always a meaning. For instance, let's have an example a bit more complex than yours :
In the context :
class pt =
object (_:'a)
method compare (x:'a) = true
end
class qt (i:int) =
object (s:'a)
val mutable state = i
method compare (x:'a) = x#get = s#get
method get = state
end
let p = new pt and q1 = new qt 1 and q2 = new qt 2;;
(** test if x is comparable to all elements of l **)
(* val compare_iter : < compare : 'a -> bool; .. > list -> 'a -> bool *)
let compare_iter l x = List.fold_left (fun b y -> b && y#compare x) true l;;
compare_iter [p;p] p;;
compare_iter [q1;q2] q1;;
Both these expressions below are not well-typed :
compare_iter [q1;q2] p;;
compare_iter [(q1:> pt);(q2:> pt)] p;;
Indeed, an object of type "qt" can not be coerced to an object of type "pt" because an object of type "qt" needs an object with a "get" method as argument for "compare". What is interesting here with ocaml polymorphism, is that you can however have a function as "compare_iter" which can both handle "lists of pt" and "lists of qt".
Hope this helps,
Sylvain.
next prev parent reply other threads:[~2001-01-11 17:39 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2001-01-10 12:39 Mattias Waldau
2001-01-10 19:16 ` Brian Rogoff
2001-01-11 9:07 ` Mattias Waldau
2001-01-11 12:26 ` Michel Schinz
2001-01-11 13:09 ` Sylvain BOULM'E [this message]
2001-01-10 19:58 ` Alain Frisch
2001-01-11 9:29 ` Sylvain BOULM'E
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=200101111309.OAA02994@baltazard.lip6.fr \
--to=sylvain.boulme@lip6.fr \
--cc=caml-list@inria.fr \
--cc=mattias.waldau@tacton.se \
/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