caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Polymorphic methods in class objects
@ 2002-08-20 20:43 Narayanan Krishnamurthy
  2002-08-20 20:46 ` [Caml-list] " Narayanan Krishnamurthy
  2002-08-20 21:56 ` [Caml-list] " Brian Rogoff
  0 siblings, 2 replies; 3+ messages in thread
From: Narayanan Krishnamurthy @ 2002-08-20 20:43 UTC (permalink / raw)
  To: caml-list; +Cc: nari

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


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

end of thread, other threads:[~2002-08-20 21:56 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-08-20 20:43 [Caml-list] Polymorphic methods in class objects Narayanan Krishnamurthy
2002-08-20 20:46 ` [Caml-list] " Narayanan Krishnamurthy
2002-08-20 21:56 ` [Caml-list] " Brian Rogoff

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