There are a number of construction-related features I'd like to see, which would be helpful if combined with partial constructors; I'm not sure how useful partial constructors would be without them.
- Ability to specify whether individual fields should be initialized before or after the base-class constructor, with the feature that late-initialized fields could reference the base object, or any portions of the object under construction which are declared higher up in the same source file.
- Ability to specify that certain fields should be initialized with constructor parameters of the same name; the declaration of any such field would require that all constructors for a class either contain a parameter with matching name and type, or chain through one which does.
- Ability to specify a variable which is like a parameter-initialized field, but may only be used within other field initializers. For example, if a class constructor accepts a `Length` parameter, which is used to size arrays `x[]`, `y[]`, and `z[]`, it would be useful if those arrays could be constructed using field-initializer syntax.
If a certain fields will be set when an object is created, and never modified thereafter, it seems much cleaner to combine the initialization and declaration, than to require that the the fields be declared in one part of the code and then initialized somewhere else. Further, the above features if combined would allow a class to initialize fields that depend upon constructor parameters before the base class constructor is called--something which is otherwise not possible.
Given the above features, I would favor a kind of parameterless partial constructors which would be bound to fields or groups of field, such that the fields could specify that they would only be written within the indicated partial constructor (a behavior even stricter than readonly
). If the value of a field will be invariant throughout the lifetime of a class object, attaching its initialization logic to the field would make that much clearer.