Jump to page: 1 2
Thread overview
Please support shorter constructor init
Jul 09
IchorDev
Jul 08
ryuukk_
Jul 08
ryuukk_
Jul 08
ryuukk_
Jul 09
Andrew
Jul 08
claptrap
Jul 09
a11e99z
July 08

Please add this and super to constructor parameters.

Old style:

 struct Rectangle {
    int x,y,width,height;
    Color color;

    this(int x, int y, int width, int height, Color color) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.color = color;
    }
}

New style. Shorter. Less to type:

struct Rectangle {
    int x,y,width,height;
    Color color;

    this(this.x, this.y, this.width, this.height, this.color) {}
}

Constructor initialization is annoying and often repeating.
Make it shorter. Nicer.

this and super can be directly supported in constructor parameters. It would save lots of typing.

The code:

module app;

class Vector2D {
    float x, y;
    this(int x, int y) {
        this.x = x;
        this.y = y;
    }
}

class Vector3D : Vector2D {
    float z;
    this(int x, int y, int z) {
        super(x,y);
        this.z = z;
    }
}

void main() {
    auto v1 = new Vector2D(10,20);
}

could be shortened to:

module app;

class Vector2D {
    float x, y;
    this(this.x, this.y);
}

class Vector3D : Vector2D {
    float z;
    this(super.x, super.y, this.z);
}

void main() {
    auto v1 = new Vector2D(10,20);
}

or ```d
module app;

class Vector2D {
float x, y;
this(this.x, this.y) {}
}

class Vector3D : Vector2D {
float z;
this(super.x, super.y, this.z) {}
}

void main() {
auto v1 = new Vector2D(10,20);
}

July 08

On Saturday, 8 July 2023 at 08:30:47 UTC, Danilo Krahn wrote:

>

Please add this and super to constructor parameters.

Old style:

 struct Rectangle {
    int x,y,width,height;
    Color color;

    this(int x, int y, int width, int height, Color color) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.color = color;
    }
}

New style. Shorter. Less to type:

struct Rectangle {
    int x,y,width,height;
    Color color;

    this(this.x, this.y, this.width, this.height, this.color) {}
}

Constructor initialization is annoying and often repeating.
Make it shorter. Nicer.

this and super can be directly supported in constructor parameters. It would save lots of typing.

For simple code, you can use my boilerplate library https://code.dlang.org/packages/boilerplate

struct Rectangle {
    int x,y,width,height;
    Color color;

    mixin(GenerateThis);
}

Rectangle(2, 3, 10, 20, color)

If the superclass is also boilerplated, it'll automatically insert the super parameters too.

Also for the love of all that is holy, use structs for vectors, not classes. The performance cost of the allocations will eat you alive.

July 08

The question was about the future of shortening the constructor syntax inside the D programming language, not about mixins or struct/class differences.

July 08

On Saturday, 8 July 2023 at 08:39:13 UTC, Danilo Krahn wrote:

>

The question was about the future of shortening the constructor syntax inside the D programming language, not about mixins or struct/class differences.

Well, I guess my answer would be "I don't think this is very important, since you can get similarly short constructors using mixins such as this library."

Honestly, I'd like the language to just generate sensible constructors to begin with. We have default values on struct and class members, so why does the autogenerated constructor allow omitting fields that don't have a default value?

The this.x syntax is very cute, I do like it (Neat uses it for its constructors) but it's sort of "neither here nor there". It's a syntax hack to make it easier to write a constructor that the language could just as easily generate on its own. Its only advantage is that it lets you reorder constructor parameters, but you can reorder class fields just as easily.

July 08

On Saturday, 8 July 2023 at 08:52:29 UTC, FeepingCreature wrote:

>

Well, I guess my answer would be "I don't think this is very important, since you can get similarly short constructors using mixins such as this library."

Your mixin doesn't let the constructor also do something else. Also you have to import the module with the mixin which is an annoyance.

>

Honestly, I'd like the language to just generate sensible constructors to begin with. We have default values on struct and class members, so why does the autogenerated constructor allow omitting fields that don't have a default value?

Because you can also omit an initializer for a local variable. If you want to restrict field values, define a constructor.

>

The this.x syntax is very cute, I do like it (Neat uses it for its constructors) but it's sort of "neither here nor there". It's a syntax hack to make it easier to write a constructor that the language could just as easily generate on its own.

Not for super.

>

Its only advantage is that it lets you reorder constructor parameters, but you can reorder class fields just as easily.

You can't reorder struct fields without changing the layout.

July 08

On Saturday, 8 July 2023 at 08:30:47 UTC, Danilo Krahn wrote:

>

Please add this and super to constructor parameters.

Note that for structs, there is no need to define a constructor if parameters map on fields one on one and the constructor does nothing else:

import std;

enum Color {Blue, Red}

struct Rectangle {
    int x,y,width,height;
    Color color;
}

void main()
{
    auto r = Rectangle(4, 5, 6, 7, Color.Blue);
    writeln(r); // Rectangle(4, 5, 6, 7, Blue)
}

And if they don't map one on one, the caller can use a static or dynamic initializer instead:

    Rectangle r2 = {width:6, height:7, x:4, y:5};
    writeln(r2); // Rectangle(4, 5, 6, 7, Blue)

For classes, though, this isn't available.

Your proposal wouldn't work in a nested class, as this and super refer to the nesting class.

-- Bastiaan.

July 08

On Saturday, 8 July 2023 at 08:30:47 UTC, Danilo Krahn wrote:

>

Please add this and super to constructor parameters.

Old style:

 struct Rectangle {
    int x,y,width,height;
    Color color;

    this(int x, int y, int width, int height, Color color) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.color = color;
    }
}

New style. Shorter. Less to type:

struct Rectangle {
    int x,y,width,height;
    Color color;

    this(this.x, this.y, this.width, this.height, this.color) {}
}

Constructor initialization is annoying and often repeating.
Make it shorter. Nicer.

this and super can be directly supported in constructor parameters. It would save lots of typing.

The code:

module app;

class Vector2D {
    float x, y;
    this(int x, int y) {
        this.x = x;
        this.y = y;
    }
}

class Vector3D : Vector2D {
    float z;
    this(int x, int y, int z) {
        super(x,y);
        this.z = z;
    }
}

void main() {
    auto v1 = new Vector2D(10,20);
}

could be shortened to:

module app;

class Vector2D {
    float x, y;
    this(this.x, this.y);
}

class Vector3D : Vector2D {
    float z;
    this(super.x, super.y, this.z);
}

void main() {
    auto v1 = new Vector2D(10,20);
}

or ```d
module app;

class Vector2D {
float x, y;
this(this.x, this.y) {}
}

class Vector3D : Vector2D {
float z;
this(super.x, super.y, this.z) {}
}

void main() {
auto v1 = new Vector2D(10,20);
}

Constructor on structs?

Vectors as classes with inheritance?

Sorry, but this is just bad advice that nobody should follow

July 08

On Saturday, 8 July 2023 at 14:52:34 UTC, ryuukk_ wrote:

>

Constructor on structs?

Vectors as classes with inheritance?

Sorry, but this is just bad advice that nobody should follow

This has nothing to do with "Vector" or "inheritance".

It's about repeating constructor initialization.

Modern languages support direct init using this and super inside the
parameters, so repeating this.x = x is not neccessary.

https://dart.dev/language#classes

July 08

On Saturday, 8 July 2023 at 15:33:34 UTC, Danilo Krahn wrote:

>

On Saturday, 8 July 2023 at 14:52:34 UTC, ryuukk_ wrote:

>

Constructor on structs?

Vectors as classes with inheritance?

Sorry, but this is just bad advice that nobody should follow

This has nothing to do with "Vector" or "inheritance".

It's about repeating constructor initialization.

Modern languages support direct init using this and super inside the
parameters, so repeating this.x = x is not neccessary.

https://dart.dev/language#classes

nobody should use constructor on structs

and nobody should use classes for basic stuffs that should be simple pod

struct vec3 {
    float x,y,z;
}

auto v = vec3(1,2,3);

This already works

vec3 v = { x: 1, y: 2, z: 3 }

This also works

July 08

On Saturday, 8 July 2023 at 08:30:47 UTC, Danilo Krahn wrote:

>

Please add this and super to constructor parameters.

To get a new feature added to the language someone needs to write a detailed "D improvement proposal" for it...

https://github.com/dlang/DIPs

It takes a lot of effort, so either you have to care enough about it to do it yourself or you need to find someone else who feels the same.

All you get by asking for a feature in the forum is a bunch people yapping about it.

Im only telling you this so you dont have unrealistic expectations regarding your request. And it is worth considering that it's probably something that's been suggested before, I dont remember for sure, but probably. And there's pretty much always bigger fish to fry.

disclaimer : Im just a user, I dont speak in any official capacity.

« First   ‹ Prev
1 2