dotnet/roslyn#73110
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
[CollectionBuilder(typeof(MyCustomCollectionFactory), nameof(MyCustomCollectionFactory.Create))]
class MyCustomCollection<T>
{
public IEnumerator<T> GetEnumerator() => default!;
}
class MyCustomCollectionFactory
{
public static MyCustomCollection<T> Create<T>(ReadOnlySpan<T> elements) => default!;
}
class Program
{
// Question: Which of these methods, should essentially be implemented as 'Create((ReadOnlySpan<int>)elements)',
// and which need to copy the 'elements' to something first (such as a new array)?
// Today, the first 2 are implemented as 'Create(elements.ToArray())', and the 3rd is an error.
public MyCustomCollection<int> Convert(Span<int> elements) => [.. elements];
public MyCustomCollection<int> Convert(int[] elements) => [.. elements];
public MyCustomCollection<int> Convert(MyArraySlice<int> elements) => [.. elements]; // error today
}
ref struct MyArraySlice<T>
{
public static implicit operator ReadOnlySpan<T>(MyArraySlice<T> value) => default!;
}
dotnet/roslyn#73110