Kenneth
01/10/2022, 3:03 PM(x != null && y == null) || (x == null && y != null)?Rahul Rawat
01/10/2022, 3:10 PMKlitos Kyriacou
01/10/2022, 3:11 PM(x == null) xor (y == null) is a simpler form, but what is your use case? It sounds like it can be made simpler than that, possibly using null-safety features. Are you, for instance, trying to get whichever of the two is not null?Kenneth
01/10/2022, 3:12 PMKenneth
01/10/2022, 3:13 PMRob Elliot
01/10/2022, 3:22 PMsealed interface Super {
val x: String?
val y: String?
}
data class X(override val x: String): Super {
override val y: String? = null
}
data class Y(override val y: String): Super {
override val x: String? = null
}Joffrey
01/10/2022, 3:24 PMx and y shouldn't even be in the Super interface at allJoffrey
01/10/2022, 3:26 PM(x == null) != (y == null) ? xor seems quite bitwise-y if you ask meMichael Langford
01/10/2022, 5:19 PMMichael Langford
01/10/2022, 5:20 PMwhen {
x==null && y!=null -> println("y is lone int")
y==null && x!=null -> println("x is lone int")
else -> println("ignore the domain issue")
}Stephan Schroeder
01/11/2022, 7:46 AMx and y shouldn't even be in the subclasses.
sealed interface Super
data class X(override val x: String): Super
data class Y(override val y: String): SuperJoffrey
01/11/2022, 9:11 AMx and y are in the subclasses in your example... so I don't get your point 🤔 How would you store the data of x and y at all if not in the subclasses?Joffrey
01/11/2022, 9:13 AMx and y should only be in their respective subclassKenneth
01/11/2022, 2:32 PMStephan Schroeder
01/12/2022, 8:13 AMoverride is no longer needed, my bad.
I have no idea how sealed classes behave in serialisation 😐
If that's your usecase maybe use a data class after all, but you can make sure the x!=null xor y!=null constraint is guaranteed
data class XOrY private constructor(
val x: String? = null,
val y: String? = null,
) {
companion object {
fun fromX(x: String) = XOrY(x=x)
fun fromY(y: String) = XOrY(y=y)
fun from(x:String?, y:String?): XOrY {
require((x==null) xor (y==null))
return XOrY(x,y)
}
}
}Joffrey
01/12/2022, 10:14 AMsealed interface (since the feature is quite recent), but I'm sure that `jackson-module-kotline`s supports sealed classes. So at worst you would just have to use a sealed class insteadRob Elliot
01/12/2022, 10:31 AMx use type X otherwise type `Y`" - have a look at https://www.baeldung.com/jackson-inheritance
For Jackson FAIL_ON_UNKNOWN_PROPERTIES is true by default, so it would fail if x and y were both present.