Repository navigation
Update for new tiebreaker rules - #4677
Conversation
There was a problem hiding this comment.
technically properties can be ref-returning and then they are lvalues and can be used as "in" arguments.
Perhaps says something like " . . . constants, ordinary properties or other expressions that produce values."
There was a problem hiding this comment.
to any field of an in parameter when it has struct type
There was a problem hiding this comment.
This restriction also applies to fields if parameter is a struct. Basically if a struct variable is readonly its fields are readonly, recursively. Not sure how to say it better.
Basically inParam.field1.field2.field3 cannot be assigned to, passed by ref, etc... , just the same as inParam itself if inParsm, field1, field2 are all structs and inParam is an in parameter.
|
Had some comments, but otherwise looks very good! |
There was a problem hiding this comment.
NIT: most understand or most easily understand ? - not sure about english.
|
closing and reopening to force a new build |
There was a problem hiding this comment.
Is it necessary to say that a temporary int is created here? Especially considering that it does not actually create a temporary local variable in IL.
There was a problem hiding this comment.
Wasn't "4-byte" correct? If not, shouldn't "8-byte" below be fixed too?
There was a problem hiding this comment.
The compiler enforces the
inargument and itsstructmembers are readonly variables.
This implies that a struct field of a class in argument should be readonly, but that's not true.
I think the explanation before that is correct, it's just the summary in the last sentence that is problematic.
rpetrusha
left a comment
There was a problem hiding this comment.
I've left a number of suggestions, comments, and questions, @BillWagner.
There was a problem hiding this comment.
the "because" doesn't seem to fully explain why it's optional; I suspect because this is the default
would explicitly --> explicitly
callsite --> call site (there are some other instances later where callsite is used as well)
There was a problem hiding this comment.
there should be a comma before "which"
There was a problem hiding this comment.
Since in is an advanced feature, this may not be an issue at all. But many developers find overload resolution confusing (or completely mysterious), so I'm wondering if it's worthwhile to add a brief explanation maybe with some simple code that asks how the compiler can choose the correct overload.
There was a problem hiding this comment.
I did some searching, and we don't have an article that explains method overloads and the basic rules in the C# docset. (I was surprised by that, and I may have missed it).
I'd rather not bury the introduction to method overloads here. I can add an issue to create that article, and make sure it's referenced from here. Do you agree?
There was a problem hiding this comment.
Yes, I definitely agree. I'll add the issue -- we really need articles for both the C# and the VB compilers.
There was a problem hiding this comment.
forces selection of --> forces the compiler to select
There was a problem hiding this comment.
temporary has --> temporary variable has
There was a problem hiding this comment.
of copies --> of making copies?
There was a problem hiding this comment.
"that" is a vague reference here.
There was a problem hiding this comment.
built in --> built-in
what are built-in numeric values? constants? numeric literals?
There was a problem hiding this comment.
I deleted "built in". (It was taken from the draft spec) It refers to the numeric types: byte, short, int, long, double and so on)
There was a problem hiding this comment.
This contradicts line 90 and following.
There was a problem hiding this comment.
PR #4710 address this (based on my review comments).
Either way, I'll update this paragraph after merging that PR.
There was a problem hiding this comment.
the original differently is correct
|
@rpetrusha I've addressed all the feedback from the earlier review, except for one comment where I had a question. Do you have recommendations as to the right guidance there? |
rpetrusha
left a comment
There was a problem hiding this comment.
This looks really good, @BillWagner. I've left one comment unrelated to your PR, one suggested change, and also noted cases where "read only" should be "read-only". You can merge when you're ready.
|
|
||
| > [!NOTE] | ||
| > The `in` keyword can also be used with a generic type parameter to specify that the type parameter is contravariant, as part of a `foreach` statement, or as part of a `join` clause in a LINQ query. For more information on the use of the `in` keyword in these contexts, see [in](in.md) which provides links to all those uses. | ||
| > The `in` keyword can also be used with a generic type parameter to specify that the type parameter is contravariant, as part of a `foreach` statement, or as part of a `join` clause in a LINQ query. For more information on the use of the `in` keyword in these contexts, see [in](in.md), which provides links to all those uses. |
There was a problem hiding this comment.
This is unrelated to your PR, but the in disambiguation topic is in the Query Keywords section in the TOC, which seems like the wrong place.
There was a problem hiding this comment.
I'm going to create a new issue for this: #4757
That way, this PR stays focused on this single topic.
|
|
||
| First, specifying `in` at the call site forces the compiler to select a method defined with a matching `in` parameter. Otherwise, when two methods differ only in the presence of `in`, the by value overload is a better match. | ||
|
|
||
| Second, specifying `in` declares your intent to pass an argument by reference. The argument used with `in` must represent a location that can be directly referred to. The same general rules for `out` and `ref` arguments apply: You cannot use constants, ordinary properties, or other expressions that produce values. Omitting `in` at the call site informs the compiler that you will allow the compiler to create a temporary variable to pass by read-only reference to the method. The compiler creates a temporary variable to overcome several restrictions with `in` arguments: |
There was a problem hiding this comment.
This remains confusing -- it was only on the fourth reading that I noticed the "Omitting...". I think that the issue can be addressed by adding "Otherwise, omitting...", which also makes it parallel to the previous paragraph.
| the value passed to it. | ||
| and `out` keywords to pass arguments | ||
| by reference. The `in` keyword specifies passing | ||
| the argument by reference but, the called method does not modify |
There was a problem hiding this comment.
the comma should precede the but, not follow it
| apply for multiple layers of member access provided the types at all levels | ||
| of member access are `structs`. | ||
| The compiler enforces that `struct` types passed as `in` arguments and their | ||
| `struct` members are read only variables when used as arguments to other methods. |
| - The argument is an expression but does not have a known storage variable. | ||
| - An overload exists that differs by the presence or absence of `in`. In that case, the by value overload is a better match. | ||
|
|
||
| These rules are useful as you update existing code to use read only |
|
|
||
| When you assign a variable to a `ref readonly return`, you can specify either a `ref readonly` | ||
| variable, or a by-value copy of the readonly reference: | ||
| variable, or a by-value copy of the read only reference: |
| Applying `ref readonly` to high-traffic uses of a struct may be sufficient. | ||
| Other times, you may want to create an immutable struct. Then you can | ||
| always pass by readonly reference. That practice | ||
| always pass by read only reference. That practice |
Fixes #3949
Fixes #3907
Update the locations where
inand by value tiebreaker rules have changed.The keys to the rules are that
inis optional at call sites. However, when it is used at call sites, the developer forces the compiler to pick theinmethod overload, and declares that the compiler must pass a reference to the argument, not a temporary created from the argument./cc @VSadov @jcouv