Deconstruct on int4 would be nice

I was trying out something compact like a shader
6631420--756268--upload_2020-12-16_23-0-39.png
to cut on the verbosity of unpacking results
6631420--756283--upload_2020-12-16_23-11-53.png
but
Assets\BlissManagerOld.cs(251,58): error CS1061: 'int4' does not contain a definition for 'Deconstruct' and no accessible extension method 'Deconstruct' accepting a first argument of type 'int4' could be found (are you missing a using directive or an assembly reference?)

1 Like

Would an extension work?

"Deconstruct " uses Tuple which is a Reference Type, so sadly it can not be used in bursted code. So even if int4 can be “Deconstruct” it will still not work.
I want to be able to use this c# feature too. maybe burst should try to replace them with ValueTuple or simply regenerate equivalent assembly using scalar values. When the team has the time…

you can deconstruct any type by having an instance or extension method with the appropriate out parameters.

thisvar (a, b, c) = foo; is just sintactic sugar for foo.Deconstruct(out var a, out var b, out var c);, and the compiler uses pattern matching (/duck typing) for this feature.

you can declare an extension method like this yourself

public void Deconstruct(this int4 value, out int x, out int y, out int z, out int w) {
    x = value.x;
    y = value.y;
    z = value.z;
    w = value.w;
}

and it will work

no.

  1. as written above, Deconstruct does not uses tuples
  2. this [icode=CSharp]var (x, y, z) = (1, 2, 3);[/icode] already uses ValueTuple for the rhs, then valueTuple.Deconstruct(...) for the assigmnents. if it is not still burstable, it would be because ValueTuple in unity .net library has LayoutKind.Auto (I last checked a long time ago, it may be fixed)

you can also provide your own struct System.ValueTuple, and the C# compiler will use that for code with tuple syntax.

2 Likes