CLOVIS
01/19/2025, 5:10 PMinterface Expression<Context : Any, out Result>
Expressions can be combined together using the typical operators;
val first: Expression<A, Int> = …
val second: Expression<A, Int> = …
val sum: Expression<A, Int> = first + second
The "context" must be the same for both operands for the operation to make sense. So far, this is simple to do.
The difficulty is the existence of a special class of values that simply ignores context. That is, if put in an operation where the other operand has a context x
, this special value behaves as if its context was also x
, and thus the result expression should have context x
. Essentially, this special value is "contextless", a kind of joker.
What is the correct variance to represent this special value?CLOVIS
01/19/2025, 5:12 PMDaniel Pitts
01/19/2025, 7:54 PMDaniel Pitts
01/19/2025, 8:01 PMinterface Expression<in Context : Any, out Result>
operator fun <Context : Any, Result> Expression<Context, Result>.plus(other: Expression<Context, Result>)
: Expression<Context, Result> = TODO()
data class Contextless<T>(val value: T) : Expression<Any, T>
fun <C:Any> foo(exp: Expression<C, Int>) {
val sum: Expression<C, Int> = exp + Contextless(1)
}
Does this work for your case?CLOVIS
01/19/2025, 8:50 PMCLOVIS
01/19/2025, 8:50 PMin Context : Any
was the solution