Hullaballoonatic
06/19/2019, 5:53 PMBar1 was also added to foos, but I imagine in other cases, that would be horrendousRuckus
06/19/2019, 5:57 PMHullaballoonatic
06/19/2019, 5:57 PMRuckus
06/19/2019, 6:01 PMBar1 not throw? You can't do '+= on nullHullaballoonatic
06/19/2019, 6:02 PMRuckus
06/19/2019, 6:05 PMBar1 isn't actually instantiated till it's used:
class EffectBuffer<P : Any>(val key: String, type: PaintType<P>) {
val blend = ShaderProperty.dynamic(
type.blendType,
"Blend",
"Blending Mode",
type.defaultBlend
)
val shaders = mutableListOf<Shader<P>>()
}
abstract class Foo(val name: String = "Foo") {
init {
println("Instantiating $name")
foos += this
println(foos)
}
override fun toString() = name
}
object Bar1 : Foo("Bar1")
val foos = mutableListOf<Foo>()
fun main() {
val bar2 = object : Foo("Bar2") {}
foos.forEach(::println)
println(Bar1.name)
}streetsofboston
06/19/2019, 6:05 PMfoos is not null. You can have forward declarations. Circular declarations may have some weird behavior, though.Hullaballoonatic
06/19/2019, 6:05 PMHullaballoonatic
06/19/2019, 6:05 PMRuckus
06/19/2019, 6:06 PMstreetsofboston
06/19/2019, 6:06 PMstreetsofboston
06/19/2019, 6:09 PMabstract class Base(val base: Base?, val id: Int) {
init {
println("Initing $base with id $id")
}
}
object A: Base(B, 1)
object B: Base(A, 2)
fun main() {
println("{$A.base} and ${B.base}")
}
Cyclical ; not sure if behavior is defined or undefined, but I guess you shouldn’t count on it 🙂Hullaballoonatic
06/19/2019, 6:10 PMRuckus
06/19/2019, 6:11 PMval is null):
object A {
val b = B
}
object B {
val a = A
}
fun main() {
println(A.b)
println(B.a)
}streetsofboston
06/19/2019, 6:12 PMabstract class Base(val base: Base?, val id: Int) {
init {
println("Initing $base with id $id")
}
}
object A: Base(B, 1)
object B: Base(A, 2)
fun main() {
println("Main")
println("{$A.base} and ${B.base}")
}
You’ll see that Main is printed before the Initing .. ones
Main
Initing null with id 2
Initing cycle.B@2c7b84de with id 1
{cycle.A@3fee733d.base} and nulllouis993546
06/19/2019, 6:12 PMBar1
https://pl.kotl.in/z0dp394JkHullaballoonatic
06/19/2019, 6:16 PMprintln(A.b) it instantiates A, which then calls B to set A.b, so now B is being instantiated, which then calls A to set B.a, which is already instantiatedstreetsofboston
06/19/2019, 6:18 PMobject initialization is a bit lazy.Hullaballoonatic
06/19/2019, 6:18 PMfun Any.ping() = this