In Kotlin I can create a generic constraint of type T; for example:
interface Foo {
  val a: String
}
class Baz<T : Foo>(x: T) {
  init {
    println(x.a)
  }
}
What I want is a generic constraint where T extends or implements multiple types.
The equivalent in TypeScript would be to use an intersection type; for example:
interface Foo {
  a: string;
}
interface Bar {
  b: number;
}
class Baz<T extends Foo & Bar> {
  constructor(x: T) {
    console.log(x.a);
    console.log(x.b);
  }
}
The equivalent in C# would be to use a generic constraint of IFoo and IBar; for example:
public interface IFoo
{
  string A { get; }
}
public interface IBar
{
  string B { get; }
}
public class Baz<T> where T : IFoo, IBar
{
  public Baz(T x)
  {
    Console.WriteLine(x.A);
    Console.WriteLine(x.B);
  }
}
Does Kotlin support an equivalent to this?
class Baz<T>(x: T) where T : Foo, T : Bar {
    init {
        println(x.a)
        println(x.b)
    }
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With