You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@optics generates bad code for sealed hierarchies with generic properties.
This is a new issue in arrow-optics 1.2.2 that wasn't present in 1.1.5.
I'm not entirely sure if I consider this an Arrow issue or a Kotlin compiler issue, since I believe it might be possible for the compiler to infer the correct types in these examples in particular. (We're using Kotlin 1.9.22 in this case.)
The following code will generate Kotlin code that doesn't compile:
@optics
sealedinterfaceBase<outT> {
val prop:Tcompanionobject
}
@optics
data classChild1(overridevalprop:String) : Base<String> {
companionobject
}
@optics
data classChild2(overridevalprop:Int) : Base<Int> {
companionobject
}
The generated code looks like this:
inlinefun <T> Base.Companion.prop(): Lens<Base<T>, T> =Lens(
get = { base:Base<T> -> base.`prop` },
set = { base:Base<T>, value:T->// Type mismatch: required return value value to be Base<T>, was Base<Any>when (base) {
// Type mismatch: required value to be String, was TisChild1-> base.copy(`prop` = value)
// Type mismatch: required value to be Int, was TisChild2-> base.copy(`prop` = value)
}
},
)
This code could be made to compile by adding some type casting:
inlinefun <T> Base.Companion.prop(): Lens<Base<T>, T> =Lens(
get = { base:Base<T> -> base.`prop` },
set = { base:Base<T>, value:T->when (base) {
isChild1-> base.copy(`prop` = value asString) asBase<T>
isChild2-> base.copy(`prop` = value asInt) asBase<T>
}
},
)
I'm not, however, sure how safe those casts are. Some rudimentary tests seem to suggest it's fine, but I haven't spent much time thinking about it:
funmain() {
val child1 =Child1("hello")
val child2 =Child2(1)
println(Base.prop<String>().get(child1)) // compilesprintln(Base.prop<Int>().get(child1)) // does not compileprintln(Base.prop<Int>().get(child2)) // compilesprintln(Base.prop<String>().get(child2)) // does not compile
}
The text was updated successfully, but these errors were encountered:
@optics
generates bad code for sealed hierarchies with generic properties.This is a new issue in arrow-optics 1.2.2 that wasn't present in 1.1.5.
I'm not entirely sure if I consider this an Arrow issue or a Kotlin compiler issue, since I believe it might be possible for the compiler to infer the correct types in these examples in particular. (We're using Kotlin 1.9.22 in this case.)
The following code will generate Kotlin code that doesn't compile:
The generated code looks like this:
This code could be made to compile by adding some type casting:
I'm not, however, sure how safe those casts are. Some rudimentary tests seem to suggest it's fine, but I haven't spent much time thinking about it:
The text was updated successfully, but these errors were encountered: