Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
* Instance variables can't be polymorphic?
@ 2009-04-05 23:16 Zheng Li
  2009-04-06  3:40 ` [Caml-list] " Jacques Garrigue
  0 siblings, 1 reply; 4+ messages in thread
From: Zheng Li @ 2009-04-05 23:16 UTC (permalink / raw)
  To: OCaml

Hi,

Here is an example:

----
# class c = object
   val iter = List.iter
end;;
class c : object val iter : ('a -> unit) -> 'a list -> unit end
----

Since iter is a instance variable, the type parameter 'a won't be 
required to parameterize the type of the class, perfect!

But it's still not polymorphic.

----
# let o = object
   inherit c
   method do_sth = iter print_int []; iter print_string []
end;;
Characters 69-81:
     method do_sth = iter print_int []; iter print_string []
                                             ^^^^^^^^^^^^
Error: This expression has type string -> unit but is here used with type
          int -> unit
----

Is that reasonable?  The inference of class c is done before the 
declaration of object o, and the type signature says it's polymorphic 
(not a weak one '_a).

Trying to declare the polymorphism explicitly as

--
val iter : 'a. ('a -> unit) -> 'a list -> unit = List.iter
-- 

won't work. This syntax is only allowed for methods.

Given that I really want to use polymorphic functions this way: as 
instance variable and accessible through inheritance, is there any 
workaround or suggestions ?

Thanks
--
Zheng


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

end of thread, other threads:[~2009-04-06  9:55 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-04-05 23:16 Instance variables can't be polymorphic? Zheng Li
2009-04-06  3:40 ` [Caml-list] " Jacques Garrigue
2009-04-06  8:35   ` Instance variables can't be polymorphic? {a few more annoyances on "val" syntax} Zheng Li
2009-04-06  9:55     ` [Caml-list] " Goswin von Brederlow

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox