Just published an article about value-based classe...
# announcements
a
Just published an article about value-based classes and using Kotlin's type system to enforce your domain rules. https://medium.com/@dev.ahmedmourad73744/value-based-classes-and-error-handling-in-kotlin-3f14727c0565?source=friends_link&sk=a16186408e1c8e317e3e11fd16e33710
👍 2
n
- are final and immutable (though may contain references to mutable objects);
👍 1
I can see now that Java is responsible for a lot of the confusion about "immutability" in the kotlin world... If you contain references to mutable types, you are not immutable
👍 1
a
Nice write-up, thanks. I'd played around with doing validation in an init block to solve the copy problem. The nocopy plugin and Either types I didn't know about, will try them. Still seems that using init is the only way to prevent copy without relying on third party dependencies.
👍 1
a
@Nir It is definitely a weird statement, as you said, having mutable properties does make you mutable. All the sample in the article are immutable, the whole thing wouldn't work if not so, will add something to point that out though.
@Adam Thanks! glad it was helpful :) Yup, without the plugin you have to rely on such options. I used to write regular classes and have equals/hashcode/toString generated, it was very ugly though.
👍 1
a
Thank you! A really useful article.
👍 1
a
@ashmelev Glad to hear :)