Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
From: Brian Rogoff <bpr@a...>
Subject: Re: [Caml-list] Polymorphic methods in class objects
Narayanan Krishnamurthy writes:
> (*********************************************)
> 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

You need a bit of explicit typing. See Section 3.10 of the manual for 
OCaml 3.06. 

# 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  : 'a . (int -> 'a) -> int -> 'a  = 
        fun 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;;
class polym :
  object
    val i : int
    method boo : unit -> float
    method private foo : (int -> 'a) -> int -> 'a
    method private func1 : int -> int
    method private func2 : int -> string
    method private func3 : int -> float
    method goo : unit -> int
    method hoo : unit -> string
  end
# let m = new polym;;
val m : polym = <obj>
# m#goo ();;    
- : int = 3
# m#hoo ();;
- : string = "2"
# m#boo ();;              
- : float = 2.

-- Brian
-------------------
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