Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Zheng Li <zheng_li@users.sourceforge.net>
To: OCaml <caml-list@yquem.inria.fr>
Subject: Instance variables can't be polymorphic?
Date: Mon, 06 Apr 2009 01:16:03 +0200	[thread overview]
Message-ID: <49D93BB3.501@users.sourceforge.net> (raw)

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


             reply	other threads:[~2009-04-05 23:14 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-04-05 23:16 Zheng Li [this message]
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

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=49D93BB3.501@users.sourceforge.net \
    --to=zheng_li@users.sourceforge.net \
    --cc=caml-list@yquem.inria.fr \
    /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