Search Unity

Feature Request Support tuple conversion and deconstruction of vector

Discussion in 'Burst' started by Thaina, Aug 10, 2021.

  1. Thaina

    Thaina

    Joined:
    Jul 13, 2012
    Posts:
    1,166
    Please add implicit conversion and deconstruct between tuple and vector

    Code (CSharp):
    1.  
    2. int3 i3 = (1,2,3);
    3. var (a,b,c) = i3; // (int,int,int)
    4. float3 f3 = (1,2,3);
    5. var (x,y,z) = f3; // (float,float,float)
    6.  
    7. (x,z) = f3.normalized.xz;
    8.  
    9. var location = new float2(a,c) + (x,z); // implicit conversion from (float,float) to float2
    10.  
     
    M_R likes this.
  2. apkdev

    apkdev

    Joined:
    Dec 12, 2015
    Posts:
    284
    It could be kinda nice, but also kinda confusing. I thought about patching them into unity.mathematics but decided I don't care enough to maintain a modified package.

    Annoyingly, there's already two ways to construct math structs -
    new float3(x, y, z)
    ,
    math.float3(x, y, z)
    - apparently to make porting of shaders easier. With C#9 target-typed expressions, now there's a third syntax -
    float3 x = new(x, y, z)
    . Not sure if we need more.

    You can use them like this, they could be useful:
    https://www.thomasclaudiushuber.com/2020/09/08/c-9-0-target-typed-new-expressions/

    As for deconstructors, you can implement them yourself for any type using extension methods. These are nice but not very IDE friendly (at least in Rider it's hard/impossible to navigate to the implementation).
    https://docs.microsoft.com/en-us/do...ruct#extension-methods-for-user-defined-types
     
    Thaina likes this.