dotnet / dotnet/csharplang

Champion: by value vs `in` overload tiebreaker (15.6, as a 7.2 bug fix)

Open
#945 32 comments 12 reactions 1 assignee Assigned to @jcouv View on GitHub
Implemented Needs ECMA Spec Proposal Proposal champion
Dominant language
C#
Stars
12.7k
Forks
1.1k
Avg merge
11h 1m
Merged PRs (30d)
3

Description

- [ ] Proposal added
- [X] Discussed in LDM
- [x] Decision in LDM
- [ ] Finalized (done, rejected, inactive)
- [ ] Spec'ed

Based on a suggestion by the LDM, we propose to add the following overload resolution tiebreaker in some future version of C#.

Two applicable methods can have identical parameter types, but differ in argument ref kind. This can occur when the ref kind of one is by value, but the other is `in`. For example, for the methods
``` c#
void M(in int x) {}
void M(int y) {}
```

These would be ambiguous at the call site
``` c#
M(expr);
```

## Proposal (was implemented in 15.6):
We propose a new tie-breaker: if there is an ambiguity and there is no in at the call-site, then the byval overload wins.
This tie-breaker comes after existing tie-breakers.

## Alternative proposal:

We propose a new tie-breaker as follows:
- If the expression is an lvalue, we bind to the overload with an `in` parameter.
- Otherwise, we bind to the overload with a value parameter.

Because it is not clear how valuable this tie-breaker would be, we are deferring it to some future time when we can judge the impact of ambiguities of this sort.

In the meantime, users can explicitly select the desired overload by using a named argument, an explicit `in` at the callsite, or by changing the declarations of the methods to be sufficiently different.

## LDM
- https://github.com/dotnet/csharplang/blob/master/meetings/2017/LDM-2017-12-04.md#in-vs-value-parameter-ambiguity

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.