how do you folks add constraints to your data clas...
# announcements
x
how do you folks add constraints to your data classes? say if parameter a is null then b cant be not null
d
Usually by using sealed classes
m
You can surely use
require()
inside the init block to check constraints for any class type: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/require.html Regarding your specific question, nullability itself is handled by the compiler by means of nullable types, so your constraint could be expressed like:
Copy code
class C(val a:Any?, val b:Any)
this tells the compiler that
a
could be null but
b
can’t. Is this of any help?
x
i think the require part is what im looking for --- I want A to be nullable, and b only to be non null if A is not null
👍 1
thank you 🙂