Version française
Home     About     Download     Resources     Contact us    
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
Hello.
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 -> List.map f [1;2;3]
end;;
  
let q = object
  method map : (int -> int) -> int list = fun f -> List.map f [1;2;3]
end;;

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,
Keiko