Suraj Shah
07/19/2019, 3:23 PMreturn@tryLambdas should throw an error here right?
It does if i make it return String directly instead of generics.
not sure what is going on here, could this be cause of type erasure?
inline fun T tryLambdas(lamb : () -> T) : T{
return lamb.invoke()
}
fun main() {
tryLambdas<String>{
return@tryLambdas // this should throw an error, but it doesnt. on decompiling it replaces it with Unit.INSTANCE
}
}Karolis
07/19/2019, 3:52 PMtryLambdas<Unit> which is a valid lambdaSuraj Shah
07/19/2019, 5:03 PMString. like i can return return@tryLambdas "some value"Karolis
07/19/2019, 5:09 PMTKarolis
07/19/2019, 5:10 PMreturn "someString", then T is String. Btw, you can omit return statement, and write just tryLambdasString { "someString" } because result of last lambda's expression is the return value of lambdaKarolis
07/19/2019, 5:10 PMSuraj Shah
07/19/2019, 5:13 PMString. so i can not return a string from a lambda which was specified to return a string. worst, i can return a string or return nothing (unit.)Suraj Shah
07/19/2019, 5:13 PMKarolis
07/19/2019, 5:16 PMtryLambdas to return if you do return from lambda?Karolis
07/19/2019, 5:16 PMSuraj Shah
07/19/2019, 5:19 PMT.Karolis
07/19/2019, 5:19 PMT, it gets inferredKarolis
07/19/2019, 5:20 PMtryLambdasString<Unit> { "string" } won't compile. Is this what you want?Karolis
07/19/2019, 5:20 PMinline fun tryLambdas(lamb : () -> Unit) : Unit {
return lamb.invoke()
}Karolis
07/19/2019, 5:22 PMinline fun <T: String> tryLambdas(lamb : () -> T) : T{
return lamb.invoke()
}
tryLambdas {
5 // does not compile, must be a String
}Suraj Shah
07/19/2019, 5:24 PMSuraj Shah
07/19/2019, 5:25 PMKarolis
07/19/2019, 5:25 PMSuraj Shah
07/19/2019, 5:26 PMtryLambdas<String> {
// allows both
return@tryLambdas "string"
// and this, which is unit,
return@tryLambdas
}
even though im specifying T as StringSuraj Shah
07/19/2019, 5:27 PMreturn@tryLambdas should throw an error right? the lambda should expect a String.Karolis
07/19/2019, 5:27 PMKarolis
07/19/2019, 5:28 PMSuraj Shah
07/19/2019, 5:29 PMKarolis
07/19/2019, 5:29 PMEnable new type inference ..., as shown in the screenshotSuraj Shah
07/19/2019, 5:30 PMSuraj Shah
07/19/2019, 5:30 PMKarolis
07/19/2019, 5:30 PMSuraj Shah
07/19/2019, 5:40 PMSuraj Shah
07/19/2019, 5:41 PM