https://kotlinlang.org logo
#language-evolution
Title
# language-evolution
s

Sam Stone

10/16/2023, 5:32 PM
Why do you have to pass overrided properties as arguments when subclassing?
Copy code
open class Parent(open val x: Int)
class Child(override val x: Int): Parent(x)
Instead of
Copy code
open class Parent(open val x: Int)
class Child(override val x: Int): Parent()
w

Wesley Hartford

10/16/2023, 5:33 PM
In most cases, I think you'd be better off not overriding:
Copy code
open class Parent(open val x: Int)
class Child(x: Int): Parent(x)
3
☝️ 1
In general, you have to pass the parameter to the constructor because it's part of the Parent's constructor. An alternative change to the code would be to make the
x
a (possibly abstract) member of parent, but not put it in the constructor.
e

ephemient

10/16/2023, 5:41 PM
Copy code
open class Parent(open val x: Int)
class Child(override val x: Int) : Parent(x = x + 1)
is legal (and confusing)
🤯 3
IMO this pattern with shadowing concrete properties is bad
if you wrote
Copy code
open class Parent {
    abstract val x: Int
}
class Child(override val x: Int) : Parent()
then there is no ambiguity and no duplication
k

Klitos Kyriacou

10/16/2023, 5:44 PM
It makes sense if you think about the equivalent Java class:
Copy code
class Parent {
    private int x;
    public int getX() { return x; }
    public Parent(int x) { this.x = x; }
}
For which you now need:
Copy code
class Child extends Parent {
    Child(int x) {
        super(x);
    }

    // You don't need the below, therefore you don't need class Child(override val x: Int)
    private int x;
    public int getX() { return x; }
}
Or, of course, you can use Ephemient's suggestion, equivalent to:
Copy code
class Parent {
    abstract int getX();
}
e

ephemient

10/16/2023, 5:46 PM
what OP wrote originally would translate into Java like
Copy code
class Parent {
    private int x;
    public int getX() { return x; }
    public Parent(int x) { this.x = x; }
}
class Child extends Parent {
    private int x;
    @Override
    public int getX() { return x; }
    public Child(int x) { super(x); }
}
where there are two different fields
x
which are not necessarily in sync with each other
either of the alternatives is better than that
s

Sam Stone

10/16/2023, 5:51 PM
Can't there be a better way to inherit a constructor with the addition of a single property? Consider the following scenarios:
Copy code
open class Parent<T>(val value: T)
class Child(override val value: Int): Parent(value)
Or
Copy code
open class Parent(val value: Int)
class Child(val value1: Int, val value2: Double): Parent(value1)
It would be nice if there was a simple way to specify the type of T (e.g.
class Child(_): Parent<Int>(_)
- and
Child(1F)
would be a type error), or add an additional property (e.g.
class Child(_, val value2: Double): Parent(_)
) and avoid the need to repeat constructors.
e

ephemient

10/16/2023, 5:55 PM
same deal as above. don't do that. pick one of
Copy code
open class Parent<T> {
    abstract val value: T
}
class Child(override val value: Int) : Parent<Int>()
Copy code
open class Parent<T>(val value: T)
class Child(value: Int) : Parent<Int>(value)
you're going to have to write
Int
at least twice either way but that is for different reasons (https://youtrack.jetbrains.com/issue/KT-43594)
s

Sam Stone

10/18/2023, 4:23 AM
Except the compiler requires writing the code in my original post if I want to make
Child
a
data class
and only have a primary constructor and name the property the same as in the parent class, because I need to add val/var. Hence, none of the suggested solutions will work (unless I want to declare abstract properties in the parent class, which is not ideal).
e

ephemient

10/18/2023, 10:18 PM
what is not ideal about abstract properties? the tradeoffs are better than a non-abstract parent with open concrete properties
d

David Kubecka

10/19/2023, 8:08 AM
Is any of the last two variants (abstract property or constructor argument) generally preferred? Or can you at least imagine concrete situations where one is more suitable than the other?