ArraySegment<T>
doesn't work well with collection expressions.
#8164
Replies: 3 comments 1 reply
-
I thought we just had a separate issue where we said we'd like to add support for |
Beta Was this translation helpful? Give feedback.
-
It was this, or falling back to creating However, we do want a "breaking change" where I also lean toward extending Create support to other types such as |
Beta Was this translation helpful? Give feedback.
-
Even |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Forked from an issue raised on dotnet/roslyn here: dotnet/roslyn#73738
The main problem is that one can successfully compiler
ArraySegment<T> a = [];
without warning, while also getting an invalid (default
) array segment back which explodes on virtually all operations.The core issue here is exactly what we ran into with
ImmutableArray<T>
. Namely that it technically fits the collection-expr pattern. It can be new'ed up, though you get hte equivalent ofnew ArraySegment<T>()
which is the same as thedefault
value of it.This discussion is being opened to help track this and discuss potential ways we could solve this. IMO, the most ideal way to support this would be to relax CollectionBuilderAttribute to allow it to point at a method taking a
T[]
not just aReadOnlySpan<T>
. Seeing that, the compiler would create an array from the collection-expression, and pass that to the creation method.We want to avoid the ROS approach as that would then force the impl to make a copy of teh span (which may already be wrapping an array the compiler created).
Beta Was this translation helpful? Give feedback.
All reactions