Search Unity

Feedback [Suggestion] Add Deconstruct instance method or extension method to Vector structs.

Discussion in 'Scripting' started by RyotaMurohoshi, Mar 12, 2019.

  1. RyotaMurohoshi

    RyotaMurohoshi

    Joined:
    Jun 20, 2013
    Posts:
    24
    In C# 7.0, Deconstructing was added.

    Please referer https://docs.microsoft.com/en-us/dotnet/csharp/deconstruct to know Deconstructing.

    ------------------------------------------------------------------------------------

    So in Unity, we can use Deconstructing for Vector structs like next code.

    Code (CSharp):
    1. var (x, y, z) = transform.position;
    2. Debug.LogFormat("{0}, {1}, {2}", x, y, z);
    Vector structs

    * Vector2
    * Vector3
    * Vector4
    * Vector2Int
    * Vector3Int

    I think this is useful. To use this Deconstructing, we need to add extension methods like next code now.

    Code (CSharp):
    1. namespace YourNameSpace
    2. {
    3.     public static class VectorExtensions
    4.     {
    5.         public static void Deconstruct(this Vector3 value, out float x, out float y, out float z)
    6.         {
    7.             x = value.x;
    8.             y = value.y;
    9.             z = value.z;
    10.         }
    11.     }
    12.  
    13.     // and more other structs...
    14. }
    ------------------------------------------------------------------------------------

    By the way, how about add Deconstructing instance method or extension methods in UnityEngine code?

    in Vector3 or other Vector structs, like next code

    instance method:

    Code (CSharp):
    1. namespace UnityEngine
    2. {
    3.     public struct Vector3
    4.     {
    5.         public static void Deconstruct(out float x, out float y, out float z)
    6.         {
    7.             x = this.x;
    8.             y = this.y;
    9.             z = this.z;
    10.         }
    11.     }
    12.  
    13.     // and more...
    14. }
    or

    extension method:

    Code (CSharp):
    1. namespace UnityEngine
    2. {
    3.     public static class VectorExtensions
    4.     {
    5.         public static void Deconstruct(this Vector3 value, out float x, out float y, out float z)
    6.         {
    7.             x = value.x;
    8.             y = value.y;
    9.             z = value.z;
    10.         }
    11.     }
    12.  
    13.     // and more other structrs...
    14. }
    I think this is so useful.
     
    AlbertZweistein, jniac, monry and 2 others like this.
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    Personally I find this to be superfluous syntax sugar that I'd probably never use... but that's just my opinion.

    ...

    With that said, as you pointed out, it already can be accomplished with a straight-forward, simple, extension method. For anyone who wants it they can add the functionality with very little effort.

    [edit]
    Sorry, I misread this as being a suggestion for Unity to add to Vector2/3.

    But you're just suggesting to people that they can add it.

    Yes, for those who would like it, very good suggestion.
     
  3. jniac

    jniac

    Joined:
    Nov 16, 2017
    Posts:
    3
    Love it, exactly what i was looking for, thx