Search Unity

Feedback Deconstruct on int4 would be nice

Discussion in 'Entity Component System' started by laurentlavigne, Dec 17, 2020.

  1. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,334
    I was trying out something compact like a shader
    upload_2020-12-16_23-0-39.png
    to cut on the verbosity of unpacking results
    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?)
     
    Last edited: Dec 17, 2020
    cultureulterior likes this.
  2. RecursiveEclipse

    RecursiveEclipse

    Joined:
    Sep 6, 2018
    Posts:
    298
    Would an extension work?
     
  3. Lieene-Guo

    Lieene-Guo

    Joined:
    Aug 20, 2013
    Posts:
    547
    "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...
     
  4. M_R

    M_R

    Joined:
    Apr 15, 2015
    Posts:
    559
    you can deconstruct any type by having an instance or extension method with the appropriate out parameters.

    this
    Code (CSharp):
    1. var (a, b, c) = foo;
    is just sintactic sugar for
    Code (CSharp):
    1. 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
    Code (CSharp):
    1. public void Deconstruct(this int4 value, out int x, out int y, out int z, out int w) {
    2.     x = value.x;
    3.     y = value.y;
    4.     z = value.z;
    5.     w = value.w;
    6. }
    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.
     
    Lieene-Guo and Sarkahn like this.