User defined convenience constructors #7722
-
I find myself writing factory functions a lot that would be better suited to be actual constructors. Take Matrix3x3(Matrix4x4)
Matrix3x3(Single,Single,Single,Single,Single,Single,Single,Single,Single)
Matrix3x3() But most of the time, you have 3 Now lets say we can write something like this: static class MyMatrixExtensions {
public convenience Matrix3x3(in Vector3 a,in Vector3 b,in Vector3 c): Matrix3x3(a.X,a.Y,a.Z,b.X,b.Y,b.Z,c.X,c.Y,c.Z); // chaining syntax like regular constructors, a convenience constructor must call an actual constructor
public convenience Matrix3x3 (Vector3[] columns) : Matrix3x3() { // uses default constructor of the struct
M11 = columns[0].X; // you can use the public properties in a convenience constructor
M21 = columns[0].Y;
M31 = columns[0].Z;
M12 = columns[1].X;
M22 = columns[1].Y;
M32 = columns[1].Z;
M13 = columns[2].X;
M23 = columns[2].Y;
M33 = columns[2].Z;
}
}
It could be implemented as syntactic sugar for a compiler generated static factory method. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
With the Role/extension proposal, public implicit extension E for Matrix3x3
{
public Matrix3x3(in Vector3 a, in Vector3 b, in Vector3 c)
: this(a.X, a.Y, a.Z, b.X, b.Y, b.Z, c.X, c.Y, c.Z)
{ }
} |
Beta Was this translation helpful? Give feedback.
With the Role/extension proposal,
May be you could add constructor to classes