在C#7中,可以将元组解构为方法参数

时间:2017-01-11 11:20:03

标签: tuples c#-7.0

例如我有

private void test(Action<ValueTuple<string, int>> fn)
{
    fn(("hello", 10));
}

test(t => 
 {
    var (s, i) = t;
    Console.WriteLine(s);
    Console.WriteLine(i);
});

我想写这样的东西

private void test(Action<ValueTuple<string, int>> fn)
{
    fn(("hello", 10));
}

test((s,i) => 
{
    Console.WriteLine(s);
    Console.WriteLine(i);
});

这有可能用一些正确的表示法吗?

7 个答案:

答案 0 :(得分:14)

您可以将其缩短为:

void test( Action<ValueTuple<string, int>> fn)
{
    fn(("hello", 10));
}

test(((string s, int i) t) =>
{
    Console.WriteLine(t.s);
    Console.WriteLine(t.i);
});

希望有一天我们可以将参数从元组映射到方法调用:

void test(Action<ValueTuple<string, int>> fn)
{
    fn(@("hello", 10)); // <-- made up syntax
}

test((s, i) =>
{
    Console.WriteLine(s);
    Console.WriteLine(i);
});

但现在不是。

答案 1 :(得分:3)

有两种查看请求的方法,但C#7.0中都不支持。

  • 一个是将元组映射到参数中:使用元组调用方法,并将元组splat的元素放入方法的不同参数中。您今天可以通过调用<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script> <canvas id="myChartTEC"></canvas> <canvas id="myChart"></canvas>手动执行此操作。
  • 另一个是lambda参数中的解构:当使用参数调用lambda时,将参数解构为元素并在lambda体中使用这些元素。您可以通过将lambda定义为M(tuple.first, tuple.second)来手动执行此操作。

csharplang design repo中正在讨论提案。

答案 2 :(得分:3)

一种选择是使用TupleSplatter(https://github.com/chartjunk/TupleSplatter):

using TupleSplatter;

void test(Action<string, int> fn)
{
    fn.SplatInvoke(("hello", 10));
    // or
    ("hello", 10).Splat(fn);
}

test((s,i) => {
    Console.WriteLine(s);
    Console.WriteLine(i);
});

答案 3 :(得分:3)

I。具有{strong> distinct-args 与单个n元组Action / Func代表的示例参数:

// 1. Action with 3 distinct 'int' parameters
Action<int, int, int> ArgsAction = (i1, i2, i3) => i1 += i2 += i3;

// 2. Func with 3 distinct 'int' parameters, returning 'long'
Func<int, int, int, long> ArgsFunc = (i1, i2, i3) => (long)i1 + i2 + i3;

// 3. Action with a single 3-tuple parameter
Action<(int, int, int)> TupleAction = args => args.Item1 += args.Item2 += args.Item3;

// 4. Action with a single 3-tuple parameter, returning 'long'
Func<(int, int, int), long> TupleFunc = args => (long)args.Item1 + args.Item2 + args.Item3;

II。演示以上示例的直接用法

long r;

// pass distinct params to multi-arg methods

ArgsAction(1, 2, 3);                // 1.

r = ArgsFunc(1, 2, 3);              // 2.

// pass tuple to tuple-taking methods

TupleAction((1, 2, 3));             // 3.

r = TupleFunc((1, 2, 3));           // 4.

下两节中的示例以其各自的非本地参数形式调用委托。要延迟方法调用或保留适合的委托以进行缓存或延迟/多次调用,请参见VI。和VII。

III。。将元组分散(“ splat”)到多参数方法中。

(1, 2, 3).Scatter(ArgsAction);      // 1.

r = (1, 2, 3).Scatter(ArgsFunc);    // 2.

IV。将不同的args传递给采用元组的方法:

TupleAction.Gather(1, 2, 3);        // 3.

r = TupleFunc.Gather(1, 2, 3);      // 4.

V。上面(III)和(IV)中使用的扩展方法ScatterGather

// disperse n-tuple into Action arguments
public static void Scatter<T0, T1>(in this (T0 i0, T1 i1) t, Action<T0, T1> a) => a(t.i0, t.i1);
public static void Scatter<T0, T1, T2>(in this (T0 i0, T1 i1, T2 i2) t, Action<T0, T1, T2> a) => a(t.i0, t.i1, t.i2);
public static void Scatter<T0, T1, T2, T3>(in this (T0 i0, T1 i1, T2 i2, T3 i3) t, Action<T0, T1, T2, T3> a) => a(t.i0, t.i1, t.i2, t.i3);

// disperse n-tuple into Func arguments
public static TResult Scatter<T0, T1, TResult>(in this (T0 i0, T1 i1) t, Func<T0, T1, TResult> f) => f(t.i0, t.i1);
public static TResult Scatter<T0, T1, T2, TResult>(in this (T0 i0, T1 i1, T2 i2) t, Func<T0, T1, T2, TResult> f) => f(t.i0, t.i1, t.i2);
public static TResult Scatter<T0, T1, T2, T3, TResult>(in this (T0 i0, T1 i1, T2 i2, T3 i3) t, Func<T0, T1, T2, T3, TResult> f) => f(t.i0, t.i1, t.i2, t.i3);

// accumulate 'n' distinct args and pass into Action as an n-tuple
public static void Gather<T0, T1>(this Action<(T0, T1)> a, T0 i0, T1 i1) => a((i0, i1));
public static void Gather<T0, T1, T2>(this Action<(T0, T1, T2)> a, T0 i0, T1 i1, T2 i2) => a((i0, i1, i2));
public static void Gather<T0, T1, T2, T3>(this Action<(T0, T1, T2, T3)> a, T0 i0, T1 i1, T2 i2, T3 i3) => a((i0, i1, i2, i3));

// accumulate 'n' distinct args and pass into Func as an n-tuple
public static TResult Gather<T0, T1, TResult>(this Func<(T0, T1), TResult> f, T0 i0, T1 i1) => f((i0, i1));
public static TResult Gather<T0, T1, T2, TResult>(this Func<(T0, T1, T2), TResult> f, T0 i0, T1 i1, T2 i2) => f((i0, i1, i2));
public static TResult Gather<T0, T1, T2, T3, TResult>(this Func<(T0, T1, T2, T3), TResult> f, T0 i0, T1 i1, T2 i2, T3 i3) => f((i0, i1, i2, i3));

VI。奖励回合。如果您打算以其备用格式多次调用元组或与众不同的参数的委托,或者尚不准备实际调用它,则可能希望从 tuple显式地预先转换该委托。 -格式添加到等效的disting-args 委托中,反之亦然。您可以缓存转换后的委托,以供以后多次或任意重用。

var ga = ArgsAction.ToGathered();        // 1.
// later...
ga((1, 2, 3));
// ...
ga((4, 5, 6));

var gf = ArgsFunc.ToGathered();          // 2.
// later...
r = gf((1, 2, 3));
// ...
r = gf((4, 5, 6));

var sa = TupleAction.ToScattered();      // 3.
// later...
sa(1, 2, 3);
// ...
sa(4, 5, 6);

var sf = TupleFunc.ToScattered();        // 4.
// later...
r = sf(1, 2, 3);
// ...
r = sf(4, 5, 6);

// of course these approaches also supports in-situ usage:

ArgsAction.ToGathered()((1, 2, 3));      // 1.
r = ArgsFunc.ToGathered()((1, 2, 3));    // 2.

TupleAction.ToScattered()(1, 2, 3);      // 3.
r = TupleFunc.ToScattered()(1, 2, 3);    // 4.

VII。 VI中显示的奖金示例的扩展方法。

// convert tuple-taking Action delegate to distinct-args form
public static Action<T0, T1> ToScattered<T0, T1>(this Action<(T0, T1)> a) => (i0, i1) => a((i0, i1));
public static Action<T0, T1, T2> ToScattered<T0, T1, T2>(this Action<(T0, T1, T2)> a) => (i0, i1, i2) => a((i0, i1, i2));
public static Action<T0, T1, T2, T3> ToScattered<T0, T1, T2, T3>(this Action<(T0, T1, T2, T3)> a) => (i0, i1, i2, i3) => a((i0, i1, i2, i3));

// convert tuple-taking Func delegate to its distinct-args form
public static Func<T0, T1, TResult> ToScattered<T0, T1, TResult>(this Func<(T0, T1), TResult> f) => (i0, i1) => f((i0, i1));
public static Func<T0, T1, T2, TResult> ToScattered<T0, T1, T2, TResult>(this Func<(T0, T1, T2), TResult> f) => (i0, i1, i2) => f((i0, i1, i2));
public static Func<T0, T1, T2, T3, TResult> ToScattered<T0, T1, T2, T3, TResult>(this Func<(T0, T1, T2, T3), TResult> f) => (i0, i1, i2, i3) => f((i0, i1, i2, i3));

// convert distinct-args Action delegate to tuple-taking form
public static Action<(T0, T1)> ToGathered<T0, T1>(this Action<T0, T1> a) => t => a(t.Item1, t.Item2);
public static Action<(T0, T1, T2)> ToGathered<T0, T1, T2>(this Action<T0, T1, T2> a) => t => a(t.Item1, t.Item2, t.Item3);
public static Action<(T0, T1, T2, T3)> ToGathered<T0, T1, T2, T3>(this Action<T0, T1, T2, T3> a) => t => a(t.Item1, t.Item2, t.Item3, t.Item4);

// convert distinct-args Func delegate to its tuple-taking form
public static Func<(T0, T1), TResult> ToGathered<T0, T1, TResult>(this Func<T0, T1, TResult> f) => t => f(t.Item1, t.Item2);
public static Func<(T0, T1, T2), TResult> ToGathered<T0, T1, T2, TResult>(this Func<T0, T1, T2, TResult> f) => t => f(t.Item1, t.Item2, t.Item3);
public static Func<(T0, T1, T2, T3), TResult> ToGathered<T0, T1, T2, T3, TResult>(this Func<T0, T1, T2, T3, TResult> f) => t => f(t.Item1, t.Item2, t.Item3, t.Item4);

答案 4 :(得分:1)

我能得到的最接近的。

public static class DeconstructExtensions
{
    public static Action<T1, T2> Deconstruct<T1, T2>(this Action<(T1, T2)> action) => (a, b) => action((a, b));
    public static Action<(T1, T2)> Construct<T1, T2>(this Action<T1, T2> action) => a => action(a.Item1, a.Item2);
}

class Test
{
    private void fn((string, int) value) { }

    private void test(Action<ValueTuple<string, int>> fn)
    {
        fn(("hello", 10));
    }

    private void Main()
    {
        var action = new Action<string, int>((s, i) =>
        {
            Console.WriteLine(s);
            Console.WriteLine(i);
        });

        test(action.Construct());
    }
}

答案 5 :(得分:1)

这里是更简洁的语法变体,不需要任何额外的导入。不,它不能解决对注释中讨论的“飞溅”语法的希望,但是没有其他答案使用ValueTuple语法作为初始参数定义。

void test(Action<(string, int)> fn)
{
    fn(("hello", 10));
}

// OR using optional named ValueTuple arguments
void test(Action<(string word, int num)> fn)
{
    fn((word: "hello", num: 10));
}

使用lambda表达式进行的调用并不那么冗长,并且仍然可以使用最少的语法来检索ValueTuple组件:

test( ((string, int) t) => {
    var (s, i) = t;

    Console.WriteLine(s);
    Console.WriteLine(i);
});

答案 6 :(得分:0)

我正在使用C#8。回调函数的一种干净方法。

public static void CallBackTest( Action<(string Name, int Age)> fn)
{
    fn(("MhamzaRajput", 23));
}

public static void Main()
{
    CallBackTest((t) =>
    {
        var ( Name, Age ) = t;

        Console.WriteLine(t.Name);
        Console.WriteLine(t.Age);
    });
}

输出

MhamzaRajput
23