caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Narayanan Krishnamurthy <Narayanan.Krishnamurthy@motorola.com>
To: caml-list@inria.fr
Cc: nari@ibmoto.com
Subject: [Caml-list] Polymorphic methods in class objects
Date: Tue, 20 Aug 2002 15:43:10 -0500	[thread overview]
Message-ID: <3D62A9DE.7C2B2CFA@motorola.com> (raw)

Hi,

This is probably a very naive question,
but I'm going to ask it anyways.
I was trying to define a polymorphic
method 'foo' as shown in the class below,
but have not been able to compile it in Ocaml
because of type unification problems.
I'm not sure why this is not possible,
since the compiler should be able to build a variant type
automatically as the value of the expression 'foo'.
Is there a fundamental reason or is it because
it is against the principle of strong typing.

Other than defining variant types to deal
with this, is there any other solution?
Any help would be greatly appreciated.
Thanx.

(*********************************************)
class polym =
  object(self)
    val i = 0
    method private func1 j = j + 1
    method private func2 j = string_of_int j
    method private func3 j = float_of_int j
    method private foo polymf n = 
      polymf n
    method goo () =
      self#foo self#func1 2
    method hoo () =
      self#foo self#func2 2
    method boo () =
      self#foo self#func3 2
  end

(* I would like to do the following *)
let m = new polym

polym#goo ()  returns 3
polym#hoo ()  returns "2"
polym#boo ()  returns "2.0"

(****************************************************)
-- 
**************************************************************************
Narayanan Krishnamurthy (Nari)
ASP Advanced Tools and Methodology
Motorola SPS Architecture and Systems Platforms
Motorola Inc. Semiconductor Products Sector
7700 W. Parmer Lane, Austin, TX 78729

Loc/MD: TX32/PL30         _
email : nari@ibmoto.com /   \ email:
Narayanan.Krishnamurthy@motorola.com 
Ph    : (512) 996-4863  |O O| Fax : (512) 996-7432 
************************| I
|*********************************************
		        \ o / 
                         ---
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


             reply	other threads:[~2002-08-20 20:43 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-08-20 20:43 Narayanan Krishnamurthy [this message]
2002-08-20 20:46 ` [Caml-list] " Narayanan Krishnamurthy
2002-08-20 21:56 ` [Caml-list] " Brian Rogoff

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=3D62A9DE.7C2B2CFA@motorola.com \
    --to=narayanan.krishnamurthy@motorola.com \
    --cc=caml-list@inria.fr \
    --cc=nari@ibmoto.com \
    /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;
as well as URLs for NNTP newsgroup(s).