Browse thread
[Caml-list] Polymorphic methods in class objects
-
Narayanan Krishnamurthy
- Narayanan Krishnamurthy
- Brian Rogoff
[
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: | 2002-08-20 (20:46) |
From: | Narayanan Krishnamurthy <Narayanan.Krishnamurthy@m...> |
Subject: | [Caml-list] Re: Polymorphic methods in class objects |
Ooops!! I'd a typo and I've changed it below. Thanx. Narayanan Krishnamurthy wrote: > > 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 > > m#goo () returns 3 > m#hoo () returns "2" > m#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 / > --- -- ************************************************************************** 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