https://kotlinlang.org logo
#announcements
Title
# announcements
i

Ifvwm

10/14/2019, 5:53 AM
Copy code
sealed class Fix<F>
data class MkFix<F> (val x: F<Fix<F>>): Fix<F>()
this is possible?
a

Amirul Zin

10/14/2019, 6:02 AM
Your data class doesn’t need declare the generic parameter for
F
and just use it as is. e.g.:
Copy code
sealed class Fix<F>
data class MkFix<F> (val x: F): Fix<F>()
i

Ifvwm

10/14/2019, 6:04 AM
@Amirul Zin no, it’s a recursive type
a

Amirul Zin

10/14/2019, 6:05 AM
Have you tried my code?
i

Ifvwm

10/14/2019, 6:06 AM
sorry, I can’t access my laptop now
a

Amirul Zin

10/14/2019, 6:07 AM
It’s the same as List<T>. e.g you can simply have:
Copy code
List<List<List<List<T>>>>
Since T is generic, so even T itself can be another parametrized generic.
m

marstran

10/14/2019, 6:08 AM
You can do this to constrain the
F
type to `Fix<F>`:
Copy code
sealed class Fix<F>
data class MkFix<F : Fix<F>> (val x: F): Fix<F>()
👍 5
i

Ifvwm

10/14/2019, 9:03 AM
and what values would have this type?
k

karelpeeters

10/14/2019, 9:10 AM
class Foo: Fix<Foo>()
i

Ifvwm

10/14/2019, 9:28 AM
and what it can be used for?
s

streetsofboston

10/14/2019, 12:29 PM
This is not possible. You can't have a type
F<A>
where
F
itself is a generic type parameter. Higher kind types are not possible on Kotlin.