Now that we have Span - another ByRefLike type - should the idea of stack only types be extended to other structs? It would allow us to encapsulate refs and Spans.
It could be like this:
If struct A has a field of _stack-only_ type it makes the struct itself _stack-only_. The same all the way up.
``` C#
struct A
{
private ref Message message; // or Span
}
struct B
{
private A a;
}
```
@KrzysztofCwalina @jkotas
This would be great because it would allow for a lot that is currently impossible (without resorting to pointers). Especially when related to closures.
The design I think makes the most sense here is to:
ref fields to the language ref types to include both ref locals, parameters and types which have ref fields. The generalized notion of ref types is more applicable here than stack only types because they actually have a number of restrictions which include stack only.
@jaredpar
Will it allow to create Span<T> fields?
@omariom yes in the cases where the containing type is a struct itself. It's not possible to create ref type fields in a class type.
There are some mitigation for Span<T> to allow it to be stored as fields of a class type. That would be specific to Span<T> though and not a general purpose feature for all ref types.
I think it would be far better to allow ref types as generic types. It would require a constraint to be added that would restrict its usage. This would be much more useful than banning them from generics.
This could be universal enumerator for types having an array as the underlying storage, like List<T>, Stack<T> etc.
```C#
stackonly struct SliceEnumerator
{
private ReadOnlySpan
private ref int version;
public ctor(ReadOnlySpan<T> span, ref int version)
{
this.span = span;
this.version = ref version;
}
}
```
This materialized as ref-like types: https://github.com/dotnet/csharplang/blob/master/proposals/span-safety.md
Most helpful comment
The design I think makes the most sense here is to:
reffields to the languageref typesto include bothreflocals, parameters and types which have ref fields.The generalized notion of
ref typesis more applicable here thanstack only typesbecause they actually have a number of restrictions which include stack only.