-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Allow by-ref Extension methods on value types #15535
Conversation
…fy that it's only used with COM
@dotnet/roslyn-compiler |
What about the combination case? (in tests) public static ref S Get(this ref S s)
{
return ref s;
} |
Thanks for this contribution! Looks like a good start to the feature. Have you taken a look at the language feature contribution guide?
Language features are a pretty involved feature as we have to consider not just the compiler but all of the other pieces of the language experience: IDE, debugging, scripting, etc ... Even for simple features this other work is often significant. This guide outlines how we approach accepting features to ensure the feature is high quality and the other work is accounted for (both for our features and those driven at least in part by the community). For this feature I don't think we need to have a formal feature specification and move onto prototyping the work. This is mostly a known feature with only a couple of undecided issues. For example:
Hence I think the next step is moving this work into a feature branch and assigning someone from the compiler team to help with the feature. I'm traveling today but should be able to get that all setup early tomorrow morning. |
This should definitely be allowed on ref returns. For non-ref r-values I think this if fine to alow because even if by-ref method mutates the structure usually it won't return it so user would be forced to introduce temporary variable anyway.
I'm not sure about usefulness of unconstrained generic case. Do you have anything on your mind?
If |
9597de4
to
ab9336d
Compare
Ref returns are l-values 😄
There is some debate to this. The main issue is that by-ref extension methods allows for observable side effects in the struct. Using a temporary means we're silently discarding the side effect. Ultimately I think that's fine though because it already happens silently today when you invoke a method on a struct in a readonly location.
Sure but it has some downstream implications that we've been discussing. In particular about the above r-value / l-value issue. If both features are present it's a reasonable stance to say:
The latter is safe because the |
Yes please 👍 Much like extensions allow observable side effects on a class; else you need to modify the struct to add the methods directly to it. An optional Question: Does this change allow C# to see VB.NET's ByRef extension methods? |
@benaadams just pushed a commit allowing discovery of VB byref methods. Did not test if it works in Intellisense yet. |
Fixes: #165
Allows adding
ref
modifier tothis
parameter in extension methods for value types.Done:
Not done:
Do you think this is valid approach to implementing this feature? What's missing from this implementation?