Shawn
06/20/2018, 3:14 PMclass Foo(val bar, val baz) {
val doober = mutableMapOf<String, String>()
val qux: Set<String>
constructor(bar, baz, lad) : this(bar, baz) {
// do things to `doober` using `lad`
qux = doober.keys.toSet()
}
}
more specifically, qux
should be an empty set if the primary constructor is called, and a computed, read-only set if the secondary constructor is called, based on the results of evaluating the parameter unique to it. I tried a separate init
block declared after the secondary constructor to populate the set using the map, but it looks like the init block gets executed before the logic in the body of the secondary constructor.
for kicks, I also tried making qux
a constructor parameter, but you can’t call the primary constructor from the secondary one using values you compute in the latteredwardwongtl
06/20/2018, 3:33 PMShawn
06/20/2018, 3:35 PMedwardwongtl
06/20/2018, 3:36 PMclass Foo {
val doober = mutableMapOf<String, String>()
val qux: Set<String>
constructor(bar: String, baz: String) {
qux = emptySet()
}
constructor(bar: String, baz: String, lad: String) {
// do things to `doober` using `lad`
qux = doober.keys.toSet()
}
}
This could workconstructor
a non-primary one, they can both initialize qux
inside the constructor.Shawn
06/20/2018, 3:39 PMbar
and baz
are `private val`s?edwardwongtl
06/20/2018, 3:40 PMclass Foo {
private val bar: String
private val baz: String
val doober = mutableMapOf<String, String>()
val qux: Set<String>
constructor(bar: String, baz: String) {
qux = emptySet()
this.bar = bar
this.baz = baz
}
constructor(bar: String, baz: String, lad: String) {
// do things to `doober` using `lad`
qux = doober.keys.toSet()
this.bar = bar
this.baz = baz
}
}
Shawn
06/20/2018, 3:40 PMedwardwongtl
06/20/2018, 3:41 PMShawn
06/20/2018, 3:41 PMedwardwongtl
06/20/2018, 3:43 PMShawn
06/20/2018, 4:42 PM?.let { ... }
to do an assignmentedwardwongtl
06/20/2018, 4:48 PM