[Feature-Request]: Allow 'required' keyword for properties in an interface #9429
Replies: 3 comments 8 replies
-
|
Beta Was this translation helpful? Give feedback.
-
I agree, we should have inheritance for Structs, abstract Structs, etc. Semantically it's weird, so you could also get rid of structs and allow classes to be allocated on the stack. Or use composition but implement syntactic sugar to batch forward methods. Compared to the pessimism in Java forums, C# is really great in that they continue adding a lot of features. However something I find frustrating is they tend to implement alternative versions (the C# way) of a language feature, which just aren't as good as the original or desired solution. This causes more features needing to be added to cover the areas the last feature missed. E.g Extension methods, default interface methods, etc. Just have Mixins or multiple implementation inheritance. It would be nice for once to just take the "risk" and go for the simpler more comprehensive solution. I think it's due to fear. I think many of the fears of software design are completely overblown. Java gained a reputation for creating huge bloated code-bases after trying to be an improvement over C++. The bad developers who blamed C++ as the problem, moved their crap to Java and then Java became the new punching bag for programmers. The issue lies with the developer(s). I have worked on large teams with Ruby and Mixins, it was never a problem. I am working on a complex project which mostly uses pointers instead of refs (ideally all pointers soon), in a multi-threaded app. I am yet to see the benefit of refs over pointers. I think I've just been brainwashed into thinking using pointers is difficult. I was also brain washed into being afraid of the Diamond problem. It's practically impossible to run into the Diamond problem if your knowledge of OO is good enough. Each domain tends to use its own language, you can only really have conflict if you're not organizing your logic into each domain properly. There are cases where multiple implementation inheritance is the semantically correct design. If your knowledge is bad, most likely the code-base will be bad no matter what. Who's seen an app almost entirely written into one class? Yes yes goto-statement I know. :P (and ye olde ADT's) |
Beta Was this translation helpful? Give feedback.
-
I want to point out that we explicitly considered and rejected |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Imagine you have an interface with 2 Properties (Text, Color) which is being used by 4 implementors, and what all these 4 have in-common is that they all NEED the
Text
Property at construction-time in order to properly function, and theColor
one is not of importance at construction time.Sadly right now I have a similar case where all 4 structs MUST be built with that
Text
Property so I need to mark in the implementor-structs each with therequired
keyword which is really tedious work..Have a look:
1. Problem - Clutter (must be repeat for every struct and is more error prone; Breaks the DRY-pattern)
2.Problem - Cant use abstract classes
3. Problem - Need to rely on the runtime to do the validation if init of the property was forgotten (less safe than on compile time of course)
First-Class-Constructs:
Structs should have 'First-Class Contracts', because they cannot make use of inheritance, so interfaces are their only mechanism for shared contracts + initialization rules and yes it is doable with Roselyn and 3rd party tools and such already but this (if deemed worthy) mustthen be set on a compiler level where it (then) would belong due to it being such a widely useful scenario.
Design-Philosphy deprecated (imho):
"interfaces should only declare behavioral, not construction rules" being able to enforce that a value HAS to be set at INITIALIZATION-TIME is still not forcing the user HOW he should create the type nor if its inside the constructor or via other (new) init construction syntaxes we have now; this is still the implementors choice and it ignores real-world needs for initialization guarantees in value types.
When the Compiler can enforce himself 'required' in all the implementors, It would prevent entire load of bugs.
The Tech is already there
Since what I propose here is already doable with usual types, adding it on interface-level should be a no-brainer
No breaking change
'required' would only be opt-in and hence existing interfaces just wouldn’t use it.
You would have:
- Guarantee: All X-structs (like in this example, 4) are initialized identically.
- Safety: Compile-time errors if Text isn’t set.
- DRY Principle: Contract defined in one place.
I hope to hear from you all and especially from the Compiler-team themselves of course and listen to some feedback :--)
Beta Was this translation helpful? Give feedback.
All reactions