Can I override a setter for a constructor `var`?
# getting-started
c
Can I override a setter for a constructor
var
?
o
No
t
@crummy you can make it a constructor parameter instead of a property and then create the property separately with a custom initialisation. class MyClass(name: String) { var name: String = name + “add something to name” }
👍 1
m
@Tom Prior why you don't use the init block ?
👍 1
t
@marco yep I would definitely use an init block if it were more complex then a one liner initialization but, for the purpose of answering @crummy question, I was just showing how to initialize a mutable property as I think that’s what he was asking..but yeah absolutely, an init block is where I would usually put primary constructor initialization code if it is more complex then a one liner to initialize a property.
👍 1