caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Why can't I use val mover : < move : int -> unit; .. > list -> unit ?
@ 2001-01-10 12:39 Mattias Waldau
  2001-01-10 19:16 ` Brian Rogoff
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Mattias Waldau @ 2001-01-10 12:39 UTC (permalink / raw)
  To: Caml-List

In the example below I have two separate classes with no common 
super class. Both classes have a method 'move'.

I have no problem using the function main below that can an 
arbitrary 'objects with move defined'. However, when I try to 
expand the example to list of objects with move defined, I 
cannot use this function.

How can I use the function 'mover'? How do I coerce to 
objects with move defined? 

/mattias




(*

let _ = mover [p;q]

This expression has type p1d_1 = < move : int -> unit >
but is here used with type
  p1d_2 = < move : int -> unit; only_here : int -> int >
Only the second object type has a method only_here

*)


class p1d_1 = 
  object 
    val mutable x = 0
    method move d = x <- x + d
  end

class p1d_2 = 
  object 
    val mutable x = 0
    method move d = x <- x + d
    method only_here x = x + x
  end

let main x = 
  x#move 3


let q = new p1d_1
let p = new p1d_2

let _ = main q
let _ = main p


let mover l = List.iter (fun x -> x#move 10) l

(* let _ = mover [p;q] *)


(*
class p1d_1 : object val mutable x : int method move : int -> unit end
class p1d_2 :
  object
    val mutable x : int
    method move : int -> unit
    method only_here : int -> int
  end
val main : < move : int -> 'a; .. > -> 'a = <fun>
val q : p1d_1 = <obj>
val p : p1d_2 = <obj>
val mover : < move : int -> unit; .. > list -> unit = <fun>

*)



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

end of thread, other threads:[~2001-01-11 17:39 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-01-10 12:39 Why can't I use val mover : < move : int -> unit; .. > list -> unit ? 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
2001-01-10 19:58 ` Alain Frisch
2001-01-11  9:29 ` Sylvain BOULM'E

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