-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
"ItemRef" - Ref element accessor for types that already have an indexer. #24110
Comments
Sounds good
|
Was a suggestion of Dictionary<TKey, TVal>
{
public ref TValue GetValueByRef(TKey key) // throws if key not found
public ref TValue TryGetValueByRef(TKey key, out bool found)
public ref TValue TryGetValueByRefOrAdd(TKey key, out bool found, TValue setVal)
} So also Might be good to have something that also works well with a |
I'm not sure how I feel about this, it just looks really weird. Other than that, would be nice to have this with specialized implementations since |
o.ItemAt(1) = 42; Could introduce named indexers in C# as per VB.NET so it could be one of o.ItemAt[1] = 42;
o.ItemRef[1] = 42;
o.ItemByRef[1] = 42;
o.ByRef[1] = 42; class MyCollection<T>
{
public T this[int index] { get; set; }
public ref T ItemByRef[int index] => throw null;
} |
Well, I've wanted named indexers since the public beta of C# 1, so my instinct is to favour that, though it opens its own can of worms. My initial thought to the opening post here was that it was another place to wish for named indexers, though. |
I think that having |
Agree. It is hard to explain and enforce for how long the ref is valid. |
Is "ref" language-neutral across .NET languages (including those who don't support ref returns, in case they do in the future)? |
VB.NET has
and F# has ref and byref;
|
Adding the helper to resizable types such as We are not discussing the set of types that could use this though. So far I hear two popular choices: |
|
|
Like what you did there 😄 |
This exact discussion came up, at length over and over again, when we did ref returns in Midori. The result is the controversy lasted until we actually checked in the change to expose the ref values from |
In "Unite Austin 2017 - Bringing Modern .NET to Unity" they even talk about how to create your own value list with |
https://github.com/dotnet/corefx/issues/25189#issuecomment-343652212
Is this at all likely in anything remotely considered short-term? My number 1 preference would be |
It is absolutely fine to do in custom collections that are optimized for specialized cases like large value types. The mainstream .NET collections are optimized for productivity, not for the last bit of performance in specialized cases. |
Currently for a value type you need to do: for (var i = 0; i < list.Count; i++)
{
var val = list[i];
val.X = 0.0;
list[i] = val;
} As well as being more perfomant, it is also more productive to mirror an array's behaviour: for (var i = 0; i < list.Count; i++)
{
list[i].X = 0.0;
} |
Getting back to the proposal though. This is meant to provide guidance for the pattern to use for ref accessors on existing types to ensure consistency when we choose to do so. The proposal isn't meant to debate which types we add it to. I'm sure we'll have plenty of fun doing that later 😄 There seem to be two suggestions now for the name: I'm slightly partial to |
Yes. I think we are nearly done here and |
What would a Something like |
While strictly speaking not an API suggestion, it's a subject we should review as part of the API review process. |
Sure. As a part of the review we need to settle on:
|
=== The list of "interesting" types :
== Not very useful / problematic
|
|
For data types like
|
FYI: The API review discussion was recorded - see https://youtu.be/o5JFZtgaJLs?t=2879 (43 min duration) |
Moving this back to api-ready-for-review to discuss @jkotas' feedback: dotnet/coreclr#17405 (comment) |
@stephentoub not sure why you re-opened this. The concern in the other PR was about other collections (not immutable ones). Did I miss anything? |
This issue is the one listing out the types to have Item/ValueRef added to it, no? |
This issue was about the naming convention (concept suffixing with Please file API review requests for the specific members so we can review those (or alternatively update the issue description to have their signatures and reopen this one). |
@terrajobst, so what did it mean for this to be tagged as |
For shorter other possible signature could be just public class List<T>
{
public ref T At(int index);
//or
public ref T Get(int index);
} Also |
We have a number of existing indexable types that could benefit from a ref-returning element accessor. For example
ImmutableArray
.A typical problem is that such types already have a get/set indexer, so the only way to add an indexed byref access to elements is to add a ref returning method.
I believe this will happen one way or another, since there is a motivation. So, I think we should standardize on the name of such method.
Here I want to propose the following convention:
The alternatives could be
Examples of uses:
Once we agree on the name/pattern we can enter separate bugs for particular APIs.
The text was updated successfully, but these errors were encountered: