Is it possible to leverage data classes while also...
# announcements
Is it possible to leverage data classes while also ensuring they are created in a consistent state (e.g. by using a factory method)? After reading the following, it seems not, but I wanted to make sure:
One recommendation:
Copy code
interface Email {
    val email: String

    companion object {
        fun create(email: String) : Email? = try { SimpleMail(email) } catch (e: Exception) { null }
private data class SimpleMail(override val email: String) : Email
The problem with this is that because the data class is private, you can’t use it in exhaustive pattern matching, which you would use if you had multiple types of email (e.g.
) common when using Algebraic Data Types
I should be clear, I’d like to avoid using Exceptions, preferring Either<E,A> instead. Or something like that
Annotate the constructor to not be used or something, only place that constructor should be used is in a functiom that returns it wrapped in an either