-
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
[Proposal] Readonly method parameter as a way to optimize value passing #3202
Comments
The one really big loophole with this is that value types can have self-mutating methods, which look and are invoked just like any other normal method. If the variable is a reference to a value type invoking such a method would modify the value type. static void Foo(readonly Point pt) { // ref is implied, per the proposed syntax above
pt.Offset(2, 2); // C# compiler has no idea that this method internally mutates the Point
}
static void Main() {
var pt = new Point(2, 2);
Foo(ref pt);
// oops, pt is now {4, 4}
} |
@HaloFour I do think the To make this work, we may need the concept of |
@whoisj The CLR has no concept of those things and they would represent breaking changes. As of now, a value type can define mutating methods and to the caller they look like any other value type instance method. The C# compiler has no idea that
|
@HaloFour hence my suggestion about const methods. |
@whoisj Which is an interesting idea but in order to actually work would require implementation and enforcement at the CLR level, as well as retrofitting across the entire BCL. |
@HaloFour would it not be just a new CLR feature? |
It could easily test this without decompiling, could it not? Assert.AreEqual(default(Point).Offset(2, 2), default(Point)); //or whatever the testing framework that ships within VS uses |
Adding runtime checks to assert would require making a copy of the value On Mon, Jun 1, 2015, 3:41 PM Joe4evr notifications@github.com wrote:
|
Rule is going to help |
Who said anything about doing the checking at runtime? I meant it should run the check in the background during design/compile time, and emit an error if it failed. |
@Joe4evr Do you suggest call function and then compare original object and object after method call? |
@KalitaAlexey Something like that. Though what kinds of factors would alter how a method behaves, exactly? |
@Joe4evr I am wrong. We don't need any checks, because if parameter passes to a method as not readonly, then this parameter will be copied as before
|
@Joe4evr Through what mechanism? The compiler doesn't execute your code as it is being compiled, and even if it could it could only do so in the simplest of scenarios. Short of CLR-enforced "pure" methods there is no way to guard against the struct being modified by its own method. |
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. See also #18002 for a general discussion of closing and/or moving language issues out of this repo. We are tracking this particular request at dotnet/csharplang#188 |
Let's consider method
On call arg contains copy of passed argument.
Let's consider adding modifier
readonly
to parameter.Now arg may contain reference
Like it was called
It is a optimization and it is preventing misunderstanding methods which accept ref parameters just for not copying
I determine some rules those method must fit:
The text was updated successfully, but these errors were encountered: