Small suggestion for default constructors

TheZipCreator thezipcreator at protonmail.com
Tue Jan 17 02:45:30 UTC 2023


Something I find quite annoying to do is to create a constructor 
that only sets the given properties. For example:

```d
class Foo {
	int x;
	int y;
	int z;
	this(int x, int y, int z) {
		this.x = x;
		this.y = y;
		this.z = z;
	}
}
```
For structs, it automatically generates a constructor, and it'd 
be nice if you could autogenerate constructors for classes too, 
kinda like this:
```d
class Foo {
	int x;
	int y;
	int z;
	default this(int x, int y, int z);
}
```
which would just be syntactic sugar for the former.

this could also be used for structs too if you've already 
specified a constructor:
```d
struct Vector3 {
	float x;
	float y;
	float z;
	this(Vector2 v) {
		x = v.x;
		y = v.y;
	}
	default this(float x, float y, float z);
}
```
I feel like this would avoid a lot of boilerplate and make things 
more concise.


More information about the Digitalmars-d mailing list