Adrian Amiel Sanchez
07/27/2023, 10:48 PMoperator fun invoke(...): Either<Exception, Foo>
. Is it an anti-pattern, given that "constructors" , by convention, should always return an instance of the object.
Studying DDD and modelling Entities and wondering where/how to have this business rules/validations.
data class Note private constructor(val note: String) {
companion object {
operator fun invoke(note: String): Either<Exception, Note> {
if (note.length > 42) {
return Either.Failure(TooLongException)
}
return Note(note)
}
}
}
spleenjack
07/27/2023, 11:30 PMimplementing invoke in a companion object to make a fake constructor is very rarely done. I do not recommend it, primarily because it violates Item 12: Use operator methods according to their names.
Youssef Shoaib [MOD]
07/28/2023, 1:26 AMCLOVIS
07/28/2023, 10:02 AMWout Werkman
07/28/2023, 12:59 PMpublic inline fun <T> Sequence(crossinline iterator: () -> Iterator<T>): Sequence<T>
Kotlin's constructor methods have the same semantics as regular functions from the outside, they aren't something special, so I'd say you're free to make it return a sum type.
By personal preference I'd even prefer a sum type over an exceptionRobert St. John
07/28/2023, 8:06 PM