5 ms·
I’m not sure we need initonly. Constructors are a design pattern that works and is intuitive at this point. Fair enough there are differences, but the overlap
by headmelted 7y ago
I’m not sure we need initonly.
Constructors are a design pattern that works and is intuitive at this point. Fair enough there are differences, but the overlap in use case is significant enough that I don’t think it warrants a rival implementation of a core language feature.
The irony of me complaining about it is that I’d much prefer we get proper object composition by letting extension methods support extended state, which would rival dependency injection in its current form.
This would overturn the apple cart even more but it’s long overdue - I implemented this years ago by using conditional weak tables but didn’t trust it even in my own work solely because if MS isn’t standing over the implementation then I’ve no way of knowing how the GC is going to handle orphaned references over time.
Example:
A car has five (obvious) wheels, only four of which have tyres and rims.
Modelling this from interfaces and injected dependencies on a parent class is a laborious anti-pattern that C# still forces me into.
I love C#, but this could be so much better.
- UK-Al05 7y agoWhat would extension methods, and extension state have over inheritance at that point? Those restrictions are in place for a reason.
- headmelted 7y agoThey are in place for a reason, but how much of the reason is due to decisions that are already set in stone is kind of my question? It's kind of agreed that object composition is a design goal we should be reaching for but are stopped frustratingly short of being able to compose objects from smaller components directly, so the design has to be top-down (class-based inheritance) rather than bottom-up (object composition). It's also cleaner than dependency injection as you wouldn't need to pre-specify what a class hierarchy can or cannot be composed of at top level.
- jimbobimbo 7y agoInitonly is a great feature that prevents ctors from growing into the unmaintainable soup of optional args, which inevitably happens as class gets used more while being worked on at the same time.
- copperx 7y agoWhat are ctors? And were they introduced with initonly?
- ResidentSleeper 7y agoThe term ctor refers to good old type constructors. It's a common abbreviation, and it's even the keyword for defining constructors in .Net IL.
- brooksyd2 7y agoConstructors
- headmelted 7y agoRight but that argument only works to say that it's better than argument soup in the constructor for injecting dependencies when (IMHO) we shouldn't be doing that in the first place anyway, as proper object composition would be much cleaner than either constructors or initonly.
- Const-me 7y agoDoes ConditionalWeakTable<TKey,TValue> class from the current .NET do what you want? It can attach arbitrary state to objects, you can do that in extension methods or any other places.
- headmelted 7y agoYeah that’s what I was referring to above - I implemented this before with CWT, but what happens to the underlying object when my reference runs out of scope? What happens to the attached state of that object if it’s collected? How is the reference held in the CIL bytecode? If composition isn’t implemented as a core language feature and officially supported then I’ve no way of knowing exactly how my implentation will behave or perform in the future or if there will be breaking changes down the road that render the foundation I’ve built all my code on moot.
- lostmsu 7y agoThis post actually looks like a speculation. These are all proposals. Have any of them been accepted at all? The syntax looks outright ugly in many examples.