Comments (14)
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.
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.
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.
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.
Here's the roslyn example that prompted this issue: dotnet/roslyn#71134
Having a dummy/unused IEnumerable
implementation seems superfluous.
from csharplang.
I guess the same feelings existed for collection expressions :-). I'll look at the example though!
from csharplang.
We explicitly (no pun intended) excluded that when we did not make a collection expression conversion a standard implicit conversion.
from csharplang.
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.
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.
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.
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.
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.
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.
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)
- More user-friendly Compile-time type checking / 更加人性化的编译时类型检查 HOT 2
- Feature Request: Syntax Sugar for AddRange in List Initialization
- [Proposal]: request support for the ISO 8601 standard 24:00:00 time format in C# HOT 2
- C#: foreach breaking when the IEnumerator.MoveNext method throws exceptions HOT 2
- [Proposal]: Dictionary expressions HOT 48
- strong typed CallerAttributes like CallerMemberName CallerArgumentExpression HOT 2
- CS8509 - Consider Exhaustiveness For Algebraic Data Types HOT 17
- [Proposal]: Implementation specific documentation HOT 2
- [Proposal]: First-Class Span Types HOT 30
- [Proposal]: Collection Expressions Next (C#13 and beyond) HOT 46
- Open issues: Breaking changes HOT 27
- [Proposal]: Field and value as contextual keywords HOT 33
- [Proposal]: Extended identifier syntax
- [API Proposal]: IsNullableAttribute to flow nullability of generic type parameters into methods HOT 4
- Anonymous type optimization HOT 5
- Feature Request: Recursively Foreach
- Compiler can't determine best common type for `switch` expression HOT 8
- [Proposal]: Relax `Add` requirement for collection expression conversions to types implementing `IEnumerable` HOT 2
- [Proposal]: Proposal for Allowing Single-Element Tuples in Type Definitions HOT 2
- [Proposal]: Params collections and older language versions
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from csharplang.