natpryce
10/11/2022, 6:46 AMfun <T> something(): T = TODO()
allowed, but val <T> something : T get() = TODO()
is not? I would think the two are equivalent.dmitriy.novozhilov
10/11/2022, 7:36 AMT
used in receiver then it's always possible to infer T
from it (because all types in receiver should be inferred before call/access itself)
But if T
is used only in return type then there will be cases when compiler can not infer it, like
val x = something // no information to infer T
Youssef Shoaib [MOD]
10/11/2022, 8:22 AMdmcg
10/12/2022, 11:04 AMYoussef Shoaib [MOD]
10/12/2022, 11:20 AMfunSomething
would simply tell you to specify a type arg. There's obviously the simple fix of just specifying the type on call site, but in some places that gets messy.
Here's a hack though. You can @Suppress(TYPE_PARAMETER_OF_PROPERTY_NOT_USED_IN_RECEIVER")
and then on the call site hint at the type somehow. E.g.:
@Suppress("TYPE_PARAMETER_OF_PROPERTY_NOT_USED_IN_RECEIVER")
val <T> something: T get() = TODO()
fun <T> coerce(x: T): T = x
fun main() {
//val x = something //Error
val x2: Int = something
val x3 = coerce<String>(something)
coerce<Boolean>(something) //even works as an unused expression!
println("Hello, world!!!")
}
Good to see you too Duncan! I've been enjoying the videos as per usual! Keep it upJavier
10/12/2022, 12:15 PMproperty<T>
if it is a getter?
The syntax doesn’t collide with functions as they do foo<T>()
dmcg
10/12/2022, 12:30 PMYoussef Shoaib [MOD]
10/12/2022, 12:39 PMMaybe Type parameter of a property can only be used as its receiver type would be a better message?
Using it as part of the receiver type is valid too. E.g.:
val <T> List<T>.first: T
dmitriy.novozhilov
10/13/2022, 6:05 AM