what would be the best way to calculate `someCompu...
# announcements
w
what would be the best way to calculate
someComputationUsingA(a)
while still being able to pass it to the super constructor and being able to access it in
FooSubType
Copy code
class FooSubType(
    val a: Int,
    val b: String
): FizzSuperType(
    someComputationUsingA(a) + " $b"
)
In the above its only passed along and I can't access the original result of the computation. I'm a little unsure about how to retain it in
FooSubType
to access at a later time
i wrote the example like this since i can't access it from the supertype since its a different form at that point
i know this doesn't work but this might make it clear about what i want to achieve something similar to
Copy code
class FooSubType(): FizzSuperType {
    val computation = someComputationUsingA(a)
    constructor(val a: Int, val b: String): FizzSuperType(computation)
}
since i can't access
this
before the supertype is initialized i am unsure what the workarounds are
d
You can use a "fake constructor" factory function: https://pl.kotl.in/P1FGsOBEs
Actually, you can do it even simpler with a secondary constructor: https://pl.kotl.in/5BR58QBNL
m
Use a secondary constructor:
Copy code
class FooSubType private constructor(val a: Int, val b: Int, val computation: Int): FizzSuperType(computation) {
    constructor(a: Int, b: Int) : this(a,b,someComputationUsingA(a))
}
Ah @diesieben07 beat me to it
w
thank you! @diesieben07 @Michael de Kaste