Let's have an intersection sugar

interface Developer {
  fun develop()
}

interface BeerExpert {
  fun drink()
}


fun <T> doAll(u: T) where T : Developer, T : BeerExpert {
  u.develop()
  u.drink()
}

// I wish that could be written as
fun doAll(u: Developer & BeerExpert) {
  u.develop()
  u.drink()
}

Inspiration: Handbook - Unions and Intersection Types

4 Likes

The syntax already exists in some situation. For example, this is legal:

fun <T : Any?> foo(u: T & Any) {
    TODO()
}

It cannot be efficiently implemented under JVM. If so, parameter in classfile must be declared as Object.

Sure. There are no generics in JVM runtime*. In most cases foo(t:T) has t:Object

But I suggest adding syntax sugar only.

*unlike C#/CLR, Rust, C++ and other languages, things like ArrayList<Integer> can’t be implemented efficiently in JVM :frowning:

1 Like