:astonished: 1.0.0 doesn't have `Coproduct`? How ...
# arrow
j
😲 1.0.0 doesn't have
Coproduct
? How come? And what's the recommended alternative?
r
Hi Julian, We dropped Coproduct because in the Kotlin 1.5 Online Event they mention that they’re planning on looking into denotable union and intersection types to the language. We did not want to carry this abstraction to 1.0. Additionally there is now new APIs for FIR compiler plugins where if Unions did not make it finally to the lang we would be able to develop ourselves for Kotlin 1.6.+ . There is no current alternative to Coproduct but creating your own hierarchy of sealed classes for it, This is usually cumbersome when you have to wrap types you don’t own like String, Int etc. but its ideal when you are talking about specific models you create as you eliminate the double wrapping. For example:
Copy code
sealed interface MyResult
data class StringValue(val value: String): MyResult
object MyCustomModel : MyResult

fun foo(): MyResult = StringValue("ok")
fun foo(): StringValue = StringValue("ok")
fun foo(): MyResult = MyCustomModel
In this case you only incur in allocation for the String case but when you use the Coproduct data type you wrap all cases. In the future we should be able to do something like
Copy code
fun foo(): String | MyCustomModel = "ok"
fun foo(): String | MyCustomModel = MyCustomModel
but we are not there yet and we dropped Coproduct because it would quickly become a bad alternative encoding to this problem. If there is anything we can do to help you migrate out of Coproduct please ping me, happy to help.
👀 1
j
Ah, okay. Thanks @raulraja! Individually wrapping types I don't own is not so bad since there really aren't that many that need that treatment. Long shot, but would you happen to know if it's possible to use 1.0 of everything except the generics library together with pre-1.0 of generics?
r
I haven’t tried it but it may be possible
👍 1