English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
[Caml-list] Polymorphic methods in class objects
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2002-08-20 (20:43)
From: Narayanan Krishnamurthy <Narayanan.Krishnamurthy@m...>
Subject: [Caml-list] Polymorphic methods in class objects

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.

class polym =
    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

(* 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:
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