Skip to content

Ability to fetch tail of an array #1

Description

@hazzik

The compiler does not allow to have 2 deconstruct methods with the same amount of arguments, so this is not possible, even when declare variable types explicitly:

public static void Deconstruct<T>(this T[] array, out T t1, out T[] rest)
{
}
public static void Deconstruct<T>(this T[] array, out T t1, out T t2)
{
}

So, we have 3 options:

  1. Do not fetch rest of the array
    var a = new [] { 1, 2, 3, 4};
    var (x,y) = a; //x=1, y=2, 3 & 4 lost in void.
  2. Fetch rest of the array as an array
    var a = new [] { 1, 2, 3, 4};
    var (x,y,z) = a; //x=1,y=2, z=new []{3,4};
    If a user does not need the rest of the array he can use _ to declare unused variable
    var (x,y,_) = a; //x=1,y=2
    The problem is, that we are unable to prevent array slicing from happening.
  3. Fetch rest of the array as an IEnumerable
    var a = new [] { 1, 2, 3, 4};
    var (x,y,z) = a; //x=1,y=1,z=lazy IEnumerable<int>
    var (x,y,_) = a; //x=1,y=2
    The idea here that rest of the array is an lazy operation which will be delayed until the values are used.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions