These two classes seem to me to behave identically:
class A constructor(ii_: Int) {
var ii = 0
var xx = 0.0F
var msg = ""
init {
ii = ii_
xx = ii + 42.0F
msg = "The value of ii is $ii and the value of xx is $xx"
}
fun display() {
print("A.display() $ii, $xx\n")
print("A.display() $msg\n")
}
}
class C {
var ii = 0
var xx = 0.0F
var msg = ""
constructor(ii_: Int) {
ii = ii_
xx = ii + 42.0F
msg = "The value of ii is $ii and the value of xx is $xx"
}
fun display() {
print("C.display() $ii, $xx\n")
print("C.display() $msg\n")
}
}
If I am not going to have more than one constructor then is there an advantage to the version that uses a primary constructor?
Is there a theoretical reason that I am not seeing for the primary ctor + init block scheme?
The primary constructor version with it's init block seems baroque, which makes me think there is a deeper reason for it, but I can't see it.
I am aware my class A does not strictly need the constructor keyword. That is not what I am asking, please do not bother telling me I don't need it.