Constructor Properties #1009
Replies: 10 comments
-
How would this work with |
Beta Was this translation helpful? Give feedback.
-
How would this idea offer any benefit over records?
|
Beta Was this translation helpful? Give feedback.
-
@DavidArno: it has overlap. but the use case is different. The primary use case here would be larger / more complex applications that usually have have some form of dependency injection and a large constructor parameter list. Records seem to be focused at simpler types and immutability. |
Beta Was this translation helpful? Give feedback.
-
The concept of constructor-based dependency injection and primary constructors would be mutually exclusive. This proposal makes even less sense in that context. Primary constructors are very specifically supposed to benefit those small data record types. You already have a fully functional constructor syntax for these larger and more complicated classes. |
Beta Was this translation helpful? Give feedback.
-
How would they be mutually exclusive? When records were last being discussed, I got the impression the team were looking at default behaviour that could be overridden. So I could use the record syntax to generate "larger / more complex applications that usually have have some form of dependency injection and a large constructor parameter list" as @mharthoorn describes: public class 4DPoint(double W, double X, double Y, double Z)
{
public double W {get; set} // overridden the default read-only property
...
// as much code as you want add
} |
Beta Was this translation helpful? Give feedback.
-
I had read it as if the goal was to provide both a dependency-injection constructor as well as a "regular" constructor which could be invoked manually to pass data values. If the intent was to just replace a dependency-injection constructor then yes, primary constructors don't exclude that possibility. |
Beta Was this translation helpful? Give feedback.
-
@DavidArno It's cool, all constructor's parameters would be public properties? Can I make X protected property? |
Beta Was this translation helpful? Give feedback.
-
To be clear, I'm in no way connected to the language design team, so anything I say here is purely my opinion and subject to being utterly ignored by the actual team. That said, from previous discussions the community and some of the team have had around the subject, their seem to be lots of enthusiasm for having minimal syntax records that had sensible defaults, but that all those defaults could be overridden if desired. Thus it would seem likely that if you declared the following: public class Point(double X, double Y)
{
protected double X { get; set; }
} then the compiler would respect the override and give you one public read-only property ( Of course, this is subject to change, eg the properties might be read/write by default, annotation might be allowed on the record declaration etc, so that the syntax to achieve the above weirdness might be: public class Point(protected double X, readonly double Y); Time will tell on this one. Also, be aware that records are currently pencilled in for v8 (subject to change, of course) and v8 is likely at least a year away from release. |
Beta Was this translation helpful? Give feedback.
-
I'd like to suggest a syntax similar to @DavidArno and already implemented in TypeScript:
Properties are combined from all the constructor. Whichever constructor is called will have their declared properties value assigned. |
Beta Was this translation helpful? Give feedback.
-
Much as a want this, I'm not a fan of the |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Allow defining a primary constructor for a class or struct by using a keyword for each parameter
This removes boilerplate code but keeps immutability on public fields. This proposal overlaps with the discussions on record types and a previous proposal for primary constructors.
The following syntax
becomes shorthand for
Of course different and possibly better keywords can be found, since it should probably be a contextual keyword, for example:
Beta Was this translation helpful? Give feedback.
All reactions