Ray Rahke
03/08/2024, 6:06 PMabstract class Base (x: Int, y: Char, z: String, q: Float) {
fun fn() {}
}
class Sub : Base {
// ^ This type has a constructor, and thus must be initialized here
}
Why can't it just, inherit the constructor, since the sub class has not given an override? Like in other languages.
This forces me to redundantly write the signature
class Sub(x: Int, y: Char, z: String, q: Float) : Base(x,y,z,q) {
}
which sucks. My use case is a 4+ depth inheritance hierarchy, where the properties are all the same. so that is 4x redundancy.Bryan Buschmann
03/08/2024, 6:15 PMclass Sub(private val a: Int, x: Int, y: Char, ...)
Bryan Buschmann
03/08/2024, 6:15 PMRay Rahke
03/08/2024, 6:16 PMRay Rahke
03/08/2024, 6:16 PMVampire
03/08/2024, 6:16 PMBryan Buschmann
03/08/2024, 6:17 PMVampire
03/08/2024, 6:17 PMRay Rahke
03/08/2024, 6:17 PMRay Rahke
03/08/2024, 6:17 PMVampire
03/08/2024, 6:17 PMRay Rahke
03/08/2024, 6:18 PMRay Rahke
03/08/2024, 6:18 PMVampire
03/08/2024, 6:18 PMVampire
03/08/2024, 6:18 PMRay Rahke
03/08/2024, 6:19 PMVampire
03/08/2024, 6:19 PMVampire
03/08/2024, 6:19 PMRay Rahke
03/08/2024, 6:19 PMRay Rahke
03/08/2024, 6:19 PMRay Rahke
03/08/2024, 6:20 PMVampire
03/08/2024, 6:20 PMVampire
03/08/2024, 6:20 PMVampire
03/08/2024, 6:20 PMVampire
03/08/2024, 6:21 PMgiven they have not provided their own constructorThat's the point, they do have a constructor
Ray Rahke
03/08/2024, 6:21 PMRay Rahke
03/08/2024, 6:22 PMRay Rahke
03/08/2024, 6:22 PMRay Rahke
03/08/2024, 6:22 PMVampire
03/08/2024, 6:22 PMRay Rahke
03/08/2024, 6:23 PMVampire
03/08/2024, 6:23 PMRay Rahke
03/08/2024, 6:29 PMRay Rahke
03/08/2024, 6:30 PMRay Rahke
03/08/2024, 6:30 PMRay Rahke
03/08/2024, 6:30 PMRay Rahke
03/08/2024, 6:31 PMVampire
03/08/2024, 6:34 PM@Vampire Sure but in those events, you already are having to define your own constructor. so the redundancy makes sense
No, I don't have to.
Vampire
03/08/2024, 6:34 PMRay Rahke
03/08/2024, 7:17 PMabstract class Base (val x: Int, val y: Char, private val z: String, private val q: Float) {
fun fn() {}
}
abstract class SubA(x: Int, y: Char, z: String, q: Float) : Base(x,y,z,q) {
}
abstract class SubB(x: Int, y: Char, z: String, q: Float) : Base(x,y,z,q) {
}
class SubASubA(x: Int, y: Char, z: String, q: Float) : SubA(x,y,z,q) {
}
class SubASubB(x: Int, y: Char, z: String, q: Float) : SubA(x,y,z,q) {
}
abstract class SubBSubA(x: Int, y: Char, z: String, q: Float) : SubB(x,y,z,q) {
}
abstract class SubBSubB(x: Int, y: Char, z: String, q: Float) : SubB(x,y,z,q) {
}
Ray Rahke
03/08/2024, 7:17 PMRay Rahke
03/08/2024, 7:17 PMRay Rahke
03/08/2024, 7:17 PMRob Elliot
03/08/2024, 7:19 PMRay Rahke
03/08/2024, 7:20 PMRay Rahke
03/08/2024, 7:20 PMRay Rahke
03/08/2024, 7:20 PMRay Rahke
03/08/2024, 7:20 PMRay Rahke
03/08/2024, 7:21 PMRay Rahke
03/08/2024, 7:21 PMRay Rahke
03/08/2024, 7:24 PMtoken
object who is a BinaryOperator
token, I should be able to say if token is Operator
, and have that return true, as BinaryOperators are a subclass of Operators. Composition does not feature this is/instanceof
operation upwards like inheritance does.Adam S
03/08/2024, 7:37 PMsealed interface Operator {
sealed interface Binary : Operator
sealed interface Unary : Operator
data object Plus : Binary
data object Minus : Binary
data object Increment : Unary
data object Negation : Unary
}
And then use a separate class to hold the data that the operators will use
class OperationData(
val x: Int,
val y: Char,
private val z: String,
private val q: Float,
)
Adam S
03/08/2024, 7:39 PMfun Operator.execute(data: OperationData): OperationResult {
return when (this) {
Operator.Minus -> TODO()
Operator.Plus -> TODO()
Operator.Increment -> TODO()
Operator.Negation -> TODO()
}
}
Or add a function to Operator, to be implemented by the subtypes
sealed interface Operator {
fun execute(data: OperationData): OperationResult
}
Ray Rahke
03/09/2024, 7:30 AMmatch(string) Boolean
method to each concreete operator?Adam S
03/09/2024, 9:56 AM