<@U88HXKLP3> forall support in Kotlin type system ...
# arrow
e
@jacob forall support in Kotlin type system is limited. Generic functions in Kotlin have forall type signature like
fun <T> foo(a: T): List<T>
(the type of
foo
in Haskell terms would be
forall t. t -> List t
), but you cannot directly capture such a functional type in a plain variable. You’ll have to always capture both
foo
and
T
with another generic function like
fun <T> bar(a: T) = foo(foo(a))
or with a generic class like
class A<T> { fun bar(a: T) = foo(a) }
In a sense,
forall
quantifier in Kotlin have to alway be on the “top level” of the type.
So in Kotlin you cannot directly have
List (forall t. t -> t)
. In order to model such a type in Kotlin you’ll have to define an interface with generic function
interface IdFun { fun <T>  id(a: T): T }
and then
List<IdFun>
would be equivalent to
List (forall t. t -> t)
👍 1
p
Nice! This is similar to HKT emulation in typeclasses: https://arrow-kt.io/docs/patterns/glossary/#using-higher-kinds-with-typeclasses
j
@elizarov thanks for the heads up 🙂 I'll give it a shot that way and see if I can make some traction