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

* [Caml-list] Re: Polymorphic methods in class objects
  2002-08-20 20:43 [Caml-list] Polymorphic methods in class objects Narayanan Krishnamurthy
@ 2002-08-20 20:46 ` Narayanan Krishnamurthy
  2002-08-20 21:56 ` [Caml-list] " Brian Rogoff
  1 sibling, 0 replies; 3+ messages in thread
From: Narayanan Krishnamurthy @ 2002-08-20 20:46 UTC (permalink / raw)
  To: caml-list

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


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

* Re: [Caml-list] Polymorphic methods in class objects
  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 ` Brian Rogoff
  1 sibling, 0 replies; 3+ messages in thread
From: Brian Rogoff @ 2002-08-20 21:56 UTC (permalink / raw)
  To: Narayanan.Krishnamurthy; +Cc: caml-list, nari

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


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