I don’t know if this is the best place, but is very verbose to put getters and seters in the constructor, so it could be more readable.
Wouldn’t it be more elegant to allow custom setters and getters this way? and continue with the idea of the primary builder
What do you think use of “override”?
class Fool (var field: Type) {
override getField(): Type {
// My custom get
return field
}
override setField(value: Type) {
// My custom validations
field = value
}
}
Yeah, but not valid logic, because not override first set of constructor and logic of set run only after setter out of constructor principal.
The only solution is:
class Fool {
constructor(prop: Type) {
this.prop = prop // Valid logic of setter, buts isn't elegant
}
var prop: Type = aDefaultValue
get() {
// My custom get
return field
}
set(value) {
// My custom validations
field = value
}
}