I want to reassign a new value in the constructor, but I can't.
class InstantDate {
private var date: LocalDateTime? = null
constructor(date: String) {
if (date.contains(" "))
{
date = date.replace(" ", "T")
this.date = LocalDateTime.parse(date)
}
}
}
I get the error "val cannot be reassigned."
How can I solve this without creating a new value? Thank you.
CodePudding user response:
Methods and constructor parameters are implicitly val
s in Kotlin and cannot be re-assigned. Either create local var
, or maybe inline your replace operation on string like so:
class InstantDate {
private var date: LocalDateTime? = null
constructor(date: String) {
if (date.contains(" ")) {
this.date = LocalDateTime.parse(date.replace(" ", "T"))
}
}
}