Coder Social home page Coder Social logo

Comments (14)

jnm2 avatar jnm2 commented on June 2, 2024 9

There was a community request for this exact issue description at #7398 (reply in thread).

Also, it would enable Memory<T> (which has no GetEnumerator) to add [CollectionBuilder] and be targeted by collection expressions without being special-cased by the compiler.

from csharplang.

RikkiGibson avatar RikkiGibson commented on June 2, 2024 4

Old collection initializers had a requirement to implement IEnumerable because without it, it was felt to be unclear if just an Add(T) taking a parameter of some type is intended for adding an element to a collection, or for e.g. performing arithmetic.

I think a type with [CollectionBuilder] not needing to be enumerable is a bit easier of a sell, as adding the attribute to the type very clearly indicates the associated Create method is intended for creating a collection. And folks who are interested in creating "write-only" collections should be able to onboard to it and get the experience they want.

from csharplang.

CyrusNajmabadi avatar CyrusNajmabadi commented on June 2, 2024 1

I see. Imo the better fix at the language level is for implicit operators to participate in collection expression target typing.

If you have an implicit operators from array, then this should ideally just work.

from csharplang.

CyrusNajmabadi avatar CyrusNajmabadi commented on June 2, 2024

Not sure how I feel about this.

Why not make it enumerable?

Seems very weird that you found use a collection to create it, but then not be able to iterate it.

from csharplang.

jcouv avatar jcouv commented on June 2, 2024

Here's the roslyn example that prompted this issue: dotnet/roslyn#71134
Having a dummy/unused IEnumerable implementation seems superfluous.

from csharplang.

CyrusNajmabadi avatar CyrusNajmabadi commented on June 2, 2024

I guess the same feelings existed for collection expressions :-). I'll look at the example though!

from csharplang.

333fred avatar 333fred commented on June 2, 2024

We explicitly (no pun intended) excluded that when we did not make a collection expression conversion a standard implicit conversion.

from csharplang.

CyrusNajmabadi avatar CyrusNajmabadi commented on June 2, 2024

We explicitly (no pun intended) excluded that when we did not make a collection expression conversion a standard implicit conversion.

Yup yup. But I'm curious if we might reconsider given some real world use case.

That said, CSharpSources seems, to me, to indicate a collection type. So not being enumerable is frankly kinda strange. I'll have to look and see how you guys actually end up using this.

from csharplang.

333fred avatar 333fred commented on June 2, 2024

I'll have to look and see how you guys actually end up using this.

We basically always realize it to a flattened array when we actually go to access it.

from csharplang.

CyrusNajmabadi avatar CyrusNajmabadi commented on June 2, 2024

so you have a collection type which is like an array, but it's all wonky on conversions to/from an array. go tit. :)

not sure how i feel about this case. seems very different than basically like everything else out there. note that i view collection-exprs as a common way to create collections. Not a nice syntax to create any arbitrary type. But i am open to this sort of relaxation because... well... it doesn't seem like it would harm anything.

from csharplang.

RikkiGibson avatar RikkiGibson commented on June 2, 2024

I thought one intended use of CollectionBuilder was to allow creation of ref structs from collection-exprs. Is that not the case? SharpLab

using System;
using System.Runtime.CompilerServices;

[CollectionBuilder(typeof(RS), nameof(RS.Create))]
public ref struct RS {
    public RS Create(ReadOnlySpan<int> items) => throw null!;
    public static void M() {
        RS rs1 = [1, 2, 3]; // error CS9188: 'RS' has a CollectionBuilderAttribute but no element type.
    }
}

from csharplang.

cston avatar cston commented on June 2, 2024

I thought one intended use of CollectionBuilder was to allow creation of ref structs from collection-exprs. Is that not the case?

It should be possible to create a ref struct collection with a pattern-based GetEnumerator() method (see sharplab.io).

[CollectionBuilder(typeof(MyCollectionBuilder), nameof(MyCollectionBuilder.Create))]
public ref struct MyCollection<T>
{
    private readonly List<T> _list;
    public MyCollection(List<T> list) { _list = list; }
    public IEnumerator<T> GetEnumerator() => _list.GetEnumerator();
}

from csharplang.

timcassell avatar timcassell commented on June 2, 2024

It would also be nice to relax the restriction for old style collection initializer syntax. An example where I wrote a class for a write-only collection initializer (before collection expressions existed): https://github.com/timcassell/ProtoBenchmarkHelpers#BenchmarkThreadHelper

from csharplang.

gulbanana avatar gulbanana commented on June 2, 2024

There was a community request for this exact issue description at #7398 (reply in thread).

Yep, I'd like to use CollectionBuilder (or some equivalent api) on a collection representing an ORM query. It's writeonly, as above - the query can be default-constructed and have elements added, but you can't iterate it until you've performed an async load or save.

from csharplang.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.