elizarov
10/01/2017, 11:39 PMtypeclass Semigroup {
fun combine(b: Self): Self
}
extension Int : Semigroup {
fun combine(b: Int): Int = this + b
}
fun add(a: Int, b: Int): Int = a.combine(b) //compiles because combine is normal extension fun for Int
fun <A : Semigroup> add(a: A, b: A): A = a.combine(b) //compiles, because every Semigroup type must have combine extension
This is a purely straw-man proposal (let’s not bikeshed about naming and declaration syntax), but see how much it is easier to use and how it nicely builds upon the Kotlin concept of extension functions.