TwoClocks
03/16/2021, 11:40 PMval
in init
or in a constructor, but not both? What's the reasoning behind this code not compiling? https://pl.kotl.in/lVxY8idN4nanodeath
03/16/2021, 11:50 PMnanodeath
03/16/2021, 11:51 PMnanodeath
03/16/2021, 11:51 PMTwoClocks
03/16/2021, 11:53 PMTwoClocks
03/16/2021, 11:55 PMnanodeath
03/16/2021, 11:56 PMShawn
03/16/2021, 11:57 PMShawn
03/16/2021, 11:57 PMShawn
03/16/2021, 11:58 PMinit
blocks as you want in a class and that they’ll run in the order they’re declared (but all before the secondary constructors)nanodeath
03/16/2021, 11:58 PMTwoClocks
03/17/2021, 12:00 AMTwoClocks
03/17/2021, 12:05 AMTwoClocks
03/17/2021, 12:06 AMTwoClocks
03/17/2021, 12:07 AMTwoClocks
03/17/2021, 12:11 AMthree = one * two
to three = 3
it compiles. It's the reference to the uninitialized vals that's the issue.nanodeath
03/17/2021, 12:12 AMShawn
03/17/2021, 12:13 AMShawn
03/17/2021, 12:14 AMoperator fun invoke
in the class’s companion object
TwoClocks
03/17/2021, 12:17 AMTwoClocks
03/17/2021, 12:24 AMthis
from init{}
which... admittedly is bad. I have shitty dependency startup issues which I punted on... I guess I need to actually think about it and try to untangle them. drat.CLOVIS
03/17/2021, 7:58 AMclass Foo(val a: Int) {
private val b: Boolean
init {
b = a == 0
}
}
An explicitely-declared constructor always runs last.
As a coding style thing, it's really rare to use secondary constructors, and when you do they should probably call the main one.CLOVIS
03/17/2021, 8:01 AMclass Foo(
val service1 = initService1(),
val service2 = initService2(),
...
)
Then, the caller can substitute whichever they want with a custom one, and you don't even need secondary constructors.TwoClocks
03/17/2021, 8:47 AMclass Foo {
val service1 = initService1( this )
...
Which makes things a little more complex.TwoClocks
03/17/2021, 8:48 AMservice
and other times I want to supply it to Foo in it's constructor.TwoClocks
03/17/2021, 8:50 AM