It would be amazing if we could reference to gener...
# language-proposals
m
It would be amazing if we could reference to generic arguments of a type.
Copy code
interface 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:
Copy code
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:
Copy code
interface OperationExecutor<TOperation : Operation<Result>, Result>

object IntToStringOperationExecutor : OperationExecutor<IntToStringOperation, Int>
(same for generic functions)
👍 1
r
I suspect JVM type erasure will prevent this (at least in non-reified types) on an implementation level, but even as syntactic sugar it would be very helpful when using classes with lots of type parameters