mikehearn
04/11/2022, 9:37 AMfun <T> bar(): T {
error("")
return "" as T
}
fun foo() {
if (true) {
1
} else {
bar()
}
}
This works - bar can be invoked, because it infers to int. But if you unwrap the if statement, suddenly the return type of bar can't be inferred anymore (the contents of bar aren't important here, this crops up no matter what it contains).Rob Elliot
04/11/2022, 10:13 AMT
as an argument, otherwise there's no point in it being generic... have you got a real world example?mikehearn
04/11/2022, 10:20 AMRob Elliot
04/11/2022, 10:21 AMUnit::class
to be the argument to that function.mikehearn
04/11/2022, 10:26 AMRob Elliot
04/11/2022, 10:56 AMT::class
. But you could, for instance, be sending the value T::class.simpleName
over the wire, or anything really.
It would be extremely surprising if val x: Int = bar()
did one thing, but bar()
by itself did something quite different.mikehearn
04/11/2022, 12:07 PMAsq
04/15/2022, 1:26 PMif
should return the type of the first common ancestor of T
and Int
, i.e. Any
. A return of Unit
means instead foo()
is a total side effect, returning no value (so it needs not be concerned in any manner with providing a result in return). Since if
is an expression (vs. a statement) it will produce a value of some type, and a value needs have a type. Unit
means, rather, something along the lines of "there is no value--of any type--associated here". Expecting that a generic, i.e. a type variable associated to some value, will resolve to Unit
, which means no value
, perhaps is a misplaced expectation?mikehearn
04/15/2022, 3:00 PMAsq
04/15/2022, 4:30 PMmikehearn
04/15/2022, 4:32 PMif (T::class == Any::class) { ... discard result .. } else if (T::class == String::class) result.asString() else ....
Asq
04/15/2022, 5:11 PMT
must be reified. But I am missing your point altogether, I'm afraid?mikehearn
04/15/2022, 5:22 PM