ref deconstrurctor #5614
Replies: 4 comments 9 replies
-
if ref fields become a thing (https://github.com/dotnet/csharplang/blob/main/proposals/low-level-struct-improvements.md) then maybe you could solve this by making Would that work for your use case? Or are AnotherStruct1 and AnotherStruct2 arbitrary user defined types? |
Beta Was this translation helpful? Give feedback.
-
They are arbitrary user defined types (e.g other structs). Not sure I follow your suggestion and how ref fields could help with deconstruct, do you have an example? |
Beta Was this translation helpful? Give feedback.
-
I also encountered a situation I needed class NavMesh
{
Status GetTileAndPolyByRef(dtPolyRef @ref, out ref MeshTile tile, out ref Poly poly);
// or
(ref MeshTile tile, ref Poly poly)? GetTileAndPolyByRef(dtPolyRef @ref);
} if (navMesh.GetTileAndPolyByRef(polyRef, out var tile, out var poly) == Status.Success) { }
// or
if (navMesh.GetTileAndPolyByRef(polyRef) is { tile: var tile, poly: var poly }) { } |
Beta Was this translation helpful? Give feedback.
-
noooooooo you can't make a ref deconstructor!!!!!!! here is a kludge bro: Lalas lalas = new(2);
foreach (var (a, b) in lalas)
{
a[0] += 5;
b[0] += 5;
}
Console.WriteLine(lalas.ass[0] + lalas.ass[1] + lalas.bss[0] + lalas.bss[1]);
struct Lalas(int n)
{
internal int[] ass = new int [n];
internal double[] bss = new double[n];
int i = -1;
int n = n;
public Lalas GetEnumerator() => this;
public Lala Current => new() { a = ass.AsSpan(i, 1), b = bss.AsSpan(i, 1) };
public bool MoveNext() => ++i < n;
public ref struct Lala
{
internal Span<int> a;
internal Span<double> b;
public void Deconstruct(out Span<int> a, out Span<double> b)
{
a = this.a;
b = this.b;
}
}
} seriously what is the big deal?? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
We have a case at Unity where we would like our users to iterate on a group of ref:
Unfortunately, a regular deconstruct pattern can't allow returning ref:
So, in our case above, we could imagine that the result of the Query function would return an
IEnumerable<MyStruct>
where MyStruct could be defined like this:The
DeconstructAttribute
would define which method or property getter can serve as a deconstruct. The first int argument defines the order (you cannot access a deconstructed1
without deconstructing0
). The second defines the used name (probably more useful for IDE assistance).Thoughts?
Beta Was this translation helpful? Give feedback.
All reactions