https://kotlinlang.org logo
Title
c

Czar

10/05/2017, 11:26 AM
I have a feature proposal, want to hear some feedback before posting to YT: Let's say we have this interface:
fun <E : Entity, ID : Serializable> getRequired(id: ID): E
We can use it either like a)
getRequired<SomeEntity,Long>(1L)
or b)
val entity: SomeEntity = getRequired(1L),
It seems to me that case a) is unnecessarily verbose, I'd like to write
getRequired<SomeEntity>(1L)
, because
1L
already contains information about type of the second generic parameter, so compiler can infer it, like it does in case b).
i

ilya.gorbunov

10/05/2017, 11:29 AM
c

Czar

10/05/2017, 11:32 AM
thanks, I'll comment on that
k

kirillrakhman

10/05/2017, 11:43 AM
there is always the possibility of an ugly workaround where you specify a useless second parameter that's just there to help type inference
getRequired(1L, of<SomeEntity>())

fun <T: Any> of() = null as T?
b

beholder

10/05/2017, 12:36 PM
The workaround can be useful if you specify second parameter as Class<E>
getRequired(1L, SomeEntity::class.java
and inside check for instance