Marc Knaup
12/16/2020, 4:24 PMinterface Box<out Value> { val value: Value }
class IntBox(override val value: Int): Box<Int>
val value: IntBox.Value = 1 // IntBox.Value is basically an alias for Int
This would be very useful in generic contexts:
interface Operation<out Result>
interface OperationExecutor<in TOperation : Operation<*>> {
suspend fun execute(operation: TOperation): TOperation.Result // <<< here
}
class IntToStringOperation(val value: Int) : Operation<String>
object IntToStringOperationExecutor : OperationExecutor<IntToStringOperation> {
override suspend fun execute(operation: IntToStringOperation): String =
operation.value.toString()
}
Currently we’d have to introduce a second type argument for that which makes generics more complicated to at the use-site:
interface OperationExecutor<TOperation : Operation<Result>, Result>
object IntToStringOperationExecutor : OperationExecutor<IntToStringOperation, Int>
(same for generic functions)rnett
12/16/2020, 11:00 PM