https://kotlinlang.org logo
Title
h

Hullaballoonatic

06/16/2019, 5:15 PM
I don't suppose an abstract class has any way to define constructors or invoke operators of its inheritors?
a

Adam Powell

06/16/2019, 5:18 PM
It seems like you're asking a lot of questions about this sort of thing - kind of deep, tightly coupled type hierarchies that involve a lot of repetition in the leaf nodes. What more specifically are you modeling?
😂 2
l

louis993546

06/16/2019, 5:28 PM
abstract class with constructor:
abstract class Foo constructor(val defaultBar: String = "default") {
    abstract fun bar(): String
}

class FooImpl : Foo() {
    override fun bar(): String = "$defaultBar bar"
}
is this what you are looking for?
d

Dominaezzz

06/16/2019, 5:30 PM
I'm guessing the ability to do
FooImpl("not default")
in addition.
👍 1
h

Hullaballoonatic

06/16/2019, 5:32 PM
At the moment, materials, e.g.
sealed class Material(override val stdValue: Money, override val stdWeight: Mass, val stdVolume: Volume) : Item() {
    abstract class Liquid(value: Money, weight: Mass, volume: Volume) : Material(value, weight, volume)
    abstract class Solid(value: Money, weight: Mass, volume: Volume) : Material(value, weight, volume)
}
so it'd be nice if everything that inherited had a constructor e.g.
class Water private constructor(val value: Money, val weight: Mass, val volume: Volume): Liquid(10.cents, 1.g, 1.mL) {
    constructor(value: Money) : this(value, (value / stdValue) * stdMass, (value / stdVolume) * stdVolume)
   // etc for other measurements
}
without having to copy paste that constructor into every inheritor
a

Adam Powell

06/16/2019, 5:34 PM
nearly every time I've gone down a road similar to this I've regretted not using composition or a flatter design
3
h

Hullaballoonatic

06/16/2019, 5:35 PM
alright, i'll try to zoom out and consider my overall approach