Syntax to invoke function and generically declare callback with tuple of the same signature #7006
-
Suppose we have function and tuple with exact same signature. Normally we need to deconstruct it and put it into function public static int DoSomething(string s,int i,long l)
{
}
static Dictionary<string,(string,int,long)> datas;
////////
public static void Main()
{
string id = "XXX";
var tuple = datas[id];
int result = DoSomething(tuple.Item1,tuple.Item2,tuple.Item3);
} I think it would be beneficial if we can invoke it with shorter syntax, something like this public static void Main()
{
string id = "XXX";
int result = DoSomething << datas[id];
} Also I wish we could declare callback that was not need to care about signature if the tuple are of the same signature Suppose there is public static void CallbackWithTuple<T>(T tuple,TupleAction<T> action)
{
action << tuple;
}
// In effect it will be like we implement all action possible for deconstructed tuple to call action
public static void CallbackWithoutTuple<T,U,V>((T,U,V) tuple,Action<T,U,V> action)
{
action(tuple.Item1,tuple.Item2,tuple.Item3);
}
public static void Main()
{
string id = "XXX";
int result = CallbackWithTuple(datas[id],DoSomething);
int result2 = CallbackWithoutTuple(datas[id],DoSomething); // only work with 1 type
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
You aren't actually using a deconstruct in your first example. If you did, then you can simply the code: // Your version
var tuple = data[id];
int result = DoSomething(tuple.Item1,tuple.Item2,tuple.Item3);
// deconstruct version
var (s, i, l) = data[id];
var result = DoSomething(s, i, l); But I agree with you that something like Python's unpack feature could make this even simpler: int result = DoSomething(*data[id]); Obviously it can't use |
Beta Was this translation helpful? Give feedback.
#1654 ?