-
Notifications
You must be signed in to change notification settings - Fork 86
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
ref readonly
is not allowed in local_variable_initializer
#831
Conversation
@Nigel-Ecma and @BillWagner : I think this sounds right - any concerns? |
@jskeet & @BillWagner – This isn’t a right/wrong issue, both the current and proposed text are valid designs. Requiring |
Generally, C# allows Possible combinations of public class C {
int a = 0;
readonly int b = 0;
public void M() {
ref int ra1 = ref a;
ref int ra2 = ref readonly a;
ref readonly int ra3 = ref a;
ref readonly int ra4 = ref readonly a;
ref int rb1 = ref b;
ref int rb2 = ref readonly b;
ref readonly int rb3 = ref b;
ref readonly int rb4 = ref readonly b;
}
} In the Roslyn implementation, the initializations of If
And the standard would need extra wording to achieve that. |
Declaring and using ref readonly locals in the C# 7.2 proposal shows the initialization |
@KalleOlaviNiemitalo – It could be said that your |
The difference between public class C {
public unsafe void M() {
int x = 1, y = 2, z = 3;
int i = 0;
ref int r = ref i == 0 ? ref x : ref (i == 1 ? ref y : ref z);
int* p = i == 0 ? &x : (i == 1 ? &y : &z);
}
} The subexpression (i == 1 ? ref y : ref z) evaluates to a reference but it still needs |
Because rb1 would be invalid in both designs, I don't understand your point. |
@KalleOlaviNiemitalo – I completely confused you as to the point I was making. Using an underscore to indicate a strong binding the compiler parses your first assignment example as:
i.e. Is this binding a language design decision or a compiler implementation choice? As I said I’d wager which one it is but I don’t know, and I don't know where Let's wait for someone who knows. |
|
I think the intention is to disallow Tagging @cston, @RikkiGibson and @jaredpar who would know for certain. |
dotnet/roslyn#118 "Proposal: Ref Returns and Locals" (from Jan 28, 2015) includes this example: public static readonly ref int Max(
readonly ref int first, readonly ref int second, readonly ref int third)
{
readonly ref int max = first > second ? ref first : ref second;
return max > third ? ref max : ref third;
} which differs from the current implementation in three ways: (1) Ref locals were mentioned in the following meeting notes and discussion issues, but those changes were not:
C# Design Meeting Sep 1 2015 shows the More brief mentions of ref locals or readonly refs: C# Language Design Notes for Feb 22, 2017 links to the C# Language Design Notes for Feb 28, 2017 introduces "Conditional operator over refs" dotnet/csharplang#223 and discusses how many C# Language Design Notes for Mar 1, 2017 continues with conditional refs and approves the C# Language Design Notes for May 31, 2017 discusses C# Language Design Notes for Jul 5, 2017 approves shipping the ref features in C# 7.2. C# Language Design Notes for Aug 28, 2017 discusses ref-like types. C# Language Design Notes for Sep 25. 2017 adds C# Language Design Notes for Sep 27, 2017 discusses argument syntax for Milestone philosophy (Oct 2, 2017) again shows I think this shows that the language design team decided to initialize with |
That is correct. This also follows to more recent features like [ref readonly] where |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unsurprisingly it has been confirmed that the “[language] design team decided the complexity of allowing a ref readonly
expression syntax didn’t outweigh the benefits and stuck with simply ref
”. So this is all good :-)
Fixes #828