If your function were to apply an arbitrary function f
to an arbitrary element of a tuple such as the function f
has the right type, then you could make this polymorphic to some extend.
In other words, if you think about what you may do with your function, you'll come up with the conclusion that you will need a function of the right type to apply it to the result of nth_diff_type
, whatever that type may be.
If we suppose for an instant that nth_diff_type
works with any tuple, its result is potentially any type. You might get an int
, a string
, or instances of more complex datatypes. Let's call that type t
. Then what can you do with a value of type t
? You may only pass it to a function which accepts values of t
.
So the problem now is to select the proper function, and that selection certainly will be done on very similar criteria than the rank of the element within your tuple. If it is so, why not simply pass your tuple, and a tuple of the functions which could be applied to nth_diff_type
, and let it do the application by itself?
let nth_diff_type i (a,b,c) (fa,fb,fc) =
match i with
| 1 -> fa a
| 2 -> fb b
| 3 -> fc c
| _ -> failwith "out of range"
-: val nth_diff_type : int -> ( 'a * 'b * 'c) -> (('a -> 'd) * ('b -> 'd) * ('c -> 'd)) -> 'd