Skip to content

[Proposal question]: Should collection expressions use implicit conversions to ReadOnlySpan for Create methods with a single spread element? #10178

Description

@RikkiGibson

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!;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Proposal QuestionQuestion to be discussed in LDM related to a proposal

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions