Semester
is a simple enum class
.
data class YearSemester(
val year: Int,
val semester: Semester
) {
constructor(string: String) : this(
string.split("-")[0].toInt(),
Semester.valueOf(string.split("-")[1])
)
}
The point is that I don't want to split the String string twice.
So to say:
data class YearSemester(
val year: Int,
val semester: Semester
) {
constructor(string: String){
val splitted = string.split("-")
this(splitted[0].toInt(), Semester.valueOf(splitted[1]))
}
}
However, the Kotlin syntax does not permit this code as the primary constructor must be called at the first of the secondary constructors.
Is there a more elegant way to handle situations like this? (Performing some complex operation before calling the primary constructor from secondary constructor)