Browse thread
Instance variables can't be polymorphic?
- Zheng Li
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Zheng Li <zheng_li@u...> |
| Subject: | Instance variables can't be polymorphic? |
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