https://kotlinlang.org logo
h

Hullaballoonatic

07/02/2019, 9:52 PM
Replace
where
syntax current:
Copy code
class Foo<A, B>(val a: A, val b: B) where A: Bar, A: Baz
proposed:
Copy code
class Foo<A: Bar & Baz, B>(val a: A, val b: B)
not like
&
is getting any use in Kotlin anyhow...
4
Also, a shoot-for-the-moon proposal: Adding
or
functionality, similar to how it is used in javascript. A quick example of how that would be really nice:
Copy code
fun <N: Byte | Int | Float | Double> areaByMetres(length: N, width: N) = Area(length.toDouble().metre, width.toDouble().metre)
To allow for
areaByMetres
to accept any number type that can be safely cast up to Double for either of its params would take 8 more declarations of the function. If we were doing this for volume, then it would take 27 more. Yikes! Of course allowing for multiple possible types has a lot more ramifications, and I imagine would be very difficult to add to a strongly typed language, and is arguably no longer strongly typed.
k

kevinmost

07/02/2019, 9:57 PM
I think the expectation is that you'd use sealed classes for unions like that. In this situation, of course, the sealed classes require an extra allocation. Maybe value types on the JVM will save us one day?
k

karelpeeters

07/02/2019, 9:58 PM
There's a lot of discussion (including comments by the Kotlin devs) here, you might be interested: https://discuss.kotlinlang.org/t/union-types/77
h

Hullaballoonatic

07/02/2019, 9:58 PM
thank you