Generic extending class AND implements interface in Kotlin

GenericsKotlin

Generics Problem Overview


Say I want a type variable, T, that extends a certain class and implements an interface. Something like:

class Foo <T : Bar implements Baz> { ... }

What is the syntax for this in Kotlin?

Generics Solutions


Solution 1 - Generics

Only one upper bound can be specified inside the angle brackets.

Kotlin offers different syntax for generic constraints when there is more than one constraint:

class Foo<T>(val t: T) where T : Bar, T : Baz { ... }

and for functions:

fun <T> f(): Foo where T : Bar, T : Baz { ... }

It is documented here.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionfrenchdonutsView Question on Stackoverflow
Solution 1 - GenericshotkeyView Answer on Stackoverflow