Gyuhyeon
02/21/2020, 6:40 AMclass A {
var x: Int = 0
var y: Int = 0
constructor(x: Int) {
this.x = x
}
constructor(x: Int, y: Int) {
this(x + y)
this.y = y
}
}
is syntax error in kotlin for whatever reasonarekolek
02/21/2020, 6:43 AMclass A {
var x: Int = 0
var y: Int = 0
constructor(x: Int) {
this.x = x
}
constructor(x: Int, y: Int) : this(x) {
this.y = y
}
}
https://kotlinlang.org/docs/reference/classes.html#secondary-constructorsGyuhyeon
02/21/2020, 6:48 AMGyuhyeon
02/21/2020, 6:48 AMGyuhyeon
02/21/2020, 6:49 AMMatej Drobnič
02/21/2020, 6:54 AMthis(x+y)
Gyuhyeon
02/21/2020, 7:01 AMthis(if(x > 0) 1 else -1)
.
I know this is a peculiar case and doesn't guarantee the original intent was even a good design in the first place, but I'm switching legacy code into kotlin and there are sometimes code like
this(param == CodeEnum.FAIL ? "A" : "B");
and I'm wondering if my only options are copy/pasting duplicate code in constructor or changing the design entirely(this whole fiasco is resulting from the .instanceBy()
static method design that I'm trying to switch into constructors)
Seems like kotlin doesn't allow this thoughMatej Drobnič
02/21/2020, 7:31 AMthis(run{ if (x > 0) 1 else -1})
Matej Drobnič
02/21/2020, 7:31 AMarekolek
02/21/2020, 7:41 AMSeems like kotlin doesn't allow this thoughI think it does 🤔 https://pl.kotl.in/uZ3lt2wqt
Gyuhyeon
02/21/2020, 8:25 AMdiesieben07
02/21/2020, 8:58 AM