-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: Infer generic constrants #8041
Comments
So a developer would not be able to see that I think that rather than silently copying the constraints from any dependencies that I'd like to see Visual Studio provide a code fix to add the constraints to the signature of the method or class that is taking dependency on those other generic constraints. |
I agree with your sentiment, but actually something like this is already happening with custom attributes #711. They can be inherited automatically without developer being fully aware of thier presence in the first place https://msdn.microsoft.com/en-us/library/system.attributeusageattribute.inherited(v=vs.110).aspx. |
@dsaf Sure, but custom attributes don't affect the signature of the targeted class/method. This would, and having the generic constraints be inferred could easily lead to an accidental break in contract leading to a |
@HaloFour for me personally custom attributes constitute a part of signature (despite any general consensus) even if in some more philosophical sense, but I don't want to sway the discussion, it's not really important here. |
@HaloFour, It could not lead to contract breaking, as it is still controlled on compilation. Moreover, all productivity tools can still show them (in tooltips, object browser, etc...). In my example, you simple can't create |
@iskiselev If other assemblies consume that assembly and a generic constraint was silently added that could break the contract as the CLR would throw an exception when enforcing that constraint at runtime. |
@HaloFour, are you talking about drop-in replacing assembly in already compiled application? I don't understand how source will help you in that case. |
The two concerns are different. In the case of breaking contracts, yes, I am referring to replacing a dependency in an already deployed application. Such can be very problematic for any system that works with plugins. My concern regarding the source code is that you can't immediately see the constraints as a part of the type/method signature. |
@HaloFour, why do you really need this see all this constraints? If we will talk a little bit more wide, method argument list is a list of constraints for method call, as you can't call method with arguments that don't match it's signature. At the same time, you don't write for each class full list of all it's base classes. Same with generic signature:
When you've declared Foo, you said, that its' generic parameter must implement
Thankfully, we don't need write |
@iskiselev |
@HaloFour, I've read your origanal comment once more and like to clarify only one additional thing: if |
This is the worse case of constraint inheritance. http://ericlippert.com/2013/07/15/why-are-generic-constraints-not-inherited/ |
I haven't thought that class signatures constraints may introduce circular reference. Looks like example with Banana, Coffee and Giraffe in Eric Lipert's article tries to create impossible hierarchy, so it is not fully honest. Even in that example, tool can show and prove it much easier, than human. So, for me it sounds even as additional pro argument for this feature.
Will be treated as compilation error per this suggestion, as only signature affect auto-inffer of generic constraints. So, you still need write:
but next will work:
At the same time, circular reference could really add complication to this, so probably, this should work only with method signatures, especially, as it already works with override methods. |
We are now taking language feature discussion on https://github.com/dotnet/csharplang for C# specific issues, https://github.com/dotnet/vblang for VB-specific features, and https://github.com/dotnet/csharplang for features that affect both languages. |
I was originally posted this suggestion in #7763, but looks like it should be separated and discussed here.
When we create any generic type/method, based on other generic type, we need to copy all generic constraints from original type. I suggest automatically infer such generic constraints implicitly, so that developer could write only additional constraints.
Suppose we have:
now, every time we want to right method, that accept
Foo<T>
we need to repeat constraintwhere T : IEnumerable
.With this we can:
The text was updated successfully, but these errors were encountered: