Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
a function for polymorphic and monomorphic objects
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Keiko Nakata <keiko@k...>
Subject: a function for polymorphic and monomorphic objects
Can I define a function taking as argument 
an object with a method, say, "map" whose type is instantiatable to
(int -> int) -> int list?

I have two object; 
one with a polymorphic map, the other with a monomorphic map.

let p = object 
  method map : 'a.(int -> 'a) -> 'a list =  fun f -> f [1;2;3]
let q = object
  method map : (int -> int) -> int list = fun f -> f [1;2;3]

I want to define a function like double below, 
which is applicable to both p and q.

let double o = o#map (fun x -> x*2);;

The only workaround I came up with is

let double2 f = f (fun x -> x*2);;
double2 p#map;;
double2 q#map;;

Yet this is not very nice....

With best regards,