[Proposal]: Extend indexers to object initializers #8826
Replies: 9 comments
-
C# does currently support index initializers using var items = new List<string>(source)
{
[^1] = "LastIsChanged"
}; be compiled into: var $temp = new List<string>(source);
var index = $temp.Count - 1;
$temp[index] = "LastIsChanged";
var list = $temp; |
Beta Was this translation helpful? Give feedback.
-
This is exactly what I'm proposing. But I'm not getting the first part of your comment (probably due to lack of understanding of how things works together internally). |
Beta Was this translation helpful? Give feedback.
-
Here's an example, shows that an indexer initializer will work if there is a compatible The problem is that |
Beta Was this translation helpful? Give feedback.
-
@HaloFour Got it! But was there a reason that the runtime didn't define this indexer instead of the compiler being faking it? |
Beta Was this translation helpful? Give feedback.
-
I believe it comes down to backward compatibility. Modifying So they introduced an index type. But modifying If you don't care about breaking existing code, many changes are possible - but you break trust with your developer community. |
Beta Was this translation helpful? Give feedback.
-
Then I think it just makes sense to "fake" things to work in initializers as well. |
Beta Was this translation helpful? Give feedback.
-
Currently, collection initializer works a bit different from dictionary and array ones. It doesn't calculate the desired length for the collection. It just calls |
Beta Was this translation helpful? Give feedback.
-
@huoyaoyuan HashSet doesn't in anyway allow indexing. So that case is unrelated. for example, the following isn't allowed. So, this proposal doesn't apply for it. using System.Collections.Generic;
var set = new HashSet<int>()
{
[0] = 5
}; But, the following is allowed, so this proposal applies to it: using System.Collections.Generic;
var set = new List<int>()
{
[0] = 5 // Ignoring the fact that this will throw - i.e, assume some collection was passed to the ctor of List<T>.
}; |
Beta Was this translation helpful? Give feedback.
-
I think we need to clarify whether this is just a hole in the "pattern index/range" implementation, or whether the current behavior is by-design. Also, the title should probably be changed to mention "object initializers", not "collection initializers". |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Indexers in collection initializers (Inspired from dotnet/roslyn#47632)
Summary
An extension to the existing collection initializers syntax.
Motivation
Indexers provide a more readable syntax to access elements based on their position from the end (
[^1]
: last element,[^2]
: element before last, etc).However, indexers doesn't work with collection initializers currently. For example the following is allowed:
But the following is not:
Detailed design
The same way the current indexers are designed based on
System.Index
.Drawbacks
Currently, there is no way to set the last element in a collection initializer, even without using indexers.
The following code fails with
CS0165
:I don't know the reason for the current limitation (possibly false positive?). But I believe this should be worked on first.
Alternatives
Unresolved questions
Design meetings
Beta Was this translation helpful? Give feedback.
All reactions