Thread overview
Override member variables
May 19, 2018
Gheorghe Gabriel
May 19, 2018
Ali Çehreli
May 20, 2018
KingJoffrey
May 20, 2018
Gheorghe Gabriel
May 19, 2018
I've worked with a lot of programming languages and I've found something interesting in Kotlin. You can override member variables. Would you like to have this feature in D?

class Rectangle {
    int width = 0;
    int height = 0;
}

class Table : Rectangle {
    override int width = 10;
    override int height = 14;
}

In my opinion it's a more cleaner sintax and you don't need to put them in constructors. When you look at the Table class definition/documentation you see that it changes width and height and their new default values. I am curious about your opinions. :)
And of course, you cannot override private members.
May 19, 2018
On 05/19/2018 11:09 AM, Gheorghe Gabriel wrote:
> I've worked with a lot of programming languages and I've found something
> interesting in Kotlin. You can override member variables. Would you like
> to have this feature in D?

It's needed in C++ and I'm sure any object-oriented programming language as well. As you seem do indicate, the current solution is to provide member functions. Assuming that the values are constant:

class Rectangle {
    int width() const {
        return 0;
    }
    int height() const {
        return 0;
    }
}

class Table : Rectangle {
    override int width() const {
        return 10;
    }
    override int height() const {
        return 14;
    }
}

void main() {
    auto r = new Table();
    assert(r.width == 10);
    assert(r.height == 14);
}

The compiler can optimize virtual function calls away when it can prove the actual type.

Here is quick exercise with current language features:

mixin template overridable(string name, T...) {
    static assert(T.length == 1, "You must provide a single value for member '" ~ name ~"'");

    import std.string : format;
    mixin (format(q{
                %s %s() const { return %s; }
            }, typeof(T[0]).stringof, name, T[0]));
}

mixin template overrided(string name, T...) {
    static assert(T.length == 1, "You must provide a single value for member '" ~ name ~"'");

    import std.string : format;
    mixin (format(q{
                override %s %s() const { return %s; }
            }, typeof(T[0]).stringof
            , name, T[0]));
}

class Rectangle {
    mixin overridable!("width", 0);
    mixin overridable!("height", 0);
}

class Table : Rectangle {
    mixin overrided!("width", 10);
    mixin overrided!("height", 14);
}

void main() {
    auto r = new Table();
    assert(r.width == 10);
    assert(r.height == 14);
}

Ali

May 20, 2018
On Saturday, 19 May 2018 at 18:09:56 UTC, Gheorghe Gabriel wrote:
> And of course, you cannot override private members.

wtf!

what do you mean we cannot override private members!

that's at the core of D!

what's with you anyway!

(ohh. to those new to the forums, that's friendly sarcasm, cause as you know, we're all friends in D - whether you like it or not).
May 20, 2018
On Sunday, 20 May 2018 at 03:02:28 UTC, KingJoffrey wrote:
> On Saturday, 19 May 2018 at 18:09:56 UTC, Gheorghe Gabriel wrote:
>> And of course, you cannot override private members.
>
> wtf!
>
> what do you mean we cannot override private members!

I mean:

module base;

class Base {
    protected int x = 1;
    private int y = 2;
}

module a;

class A : Base {
    override int x = 7;
    override int y = 12; // error: y is not accessible
}