Hi All
How can I type a function argument that operates on containers and is applied to multiple containers inside the function:
let fn = (a: (forall 'x. array<'x> => 'x), lst: array<'a>, rst: array<'b>) => (a(lst), b(lst))
I am sure ive seen some talk of this here and some evidence of it in reasonml but cant find the examples here on in docs…
Thanks!
Alex
johnj
3
This is a limitation of higher-rank polymorphic functions. The OCaml documentation has a section about it here.
The short answer is that this kind of polymorphism is not directly possible, but it can be achieved by wrapping the function in a new type.
@unboxed
type polyFn<'a> = {f: 'a. array<'a> => 'a}
let fn = ({f}, a, b) => (f(a), f(b))
let x = fn({f: a => a[0]}, [1, 2], ["a", "b"])
The @unboxed
annotation is not necessary, but it will let the compiler optimize away the record so the output just has the function.
4 Likes
Ahh there it is, of course…
I was definitely looking through your previous posts john.
Thanks again,
Alex
1 Like