Discussion: Auto-splatting #1654
Replies: 14 comments
-
I love it! |
Beta Was this translation helpful? Give feedback.
-
I think I'd be more comfortable if we could make it explicit with a
That said, I feel like this feature very quickly makes code difficult to read (especially with mixing arguments or expanding multiple tuples) and so am not sure if it'd be worth adding to the language. |
Beta Was this translation helpful? Give feedback.
-
I'm with @scalablecory on this; doing it implicitly makes me nervous and I'd prefer an explicit operator to say "splat this Deconstructible here". I was going to suggest prefix |
Beta Was this translation helpful? Give feedback.
-
You could easily construct situations where this will lead to ambiguity (if not for the compiler, then at least for the developer reading the code). I think adding auto-magical behavior requires an even higher bar than normal. I do think adding a splat operator makes the feature a lot more palatable. But I'm still not convinced this is common enough for the benefit to be worth the cost. |
Beta Was this translation helpful? Give feedback.
-
The first Besides that, this seems a very natural extension of tuples, given how C# syntax for them mirrors parameter syntax (which was on purpose). Great idea! |
Beta Was this translation helpful? Give feedback.
-
I'm seriously torn on this. A year ago, I'd have loved this: "yes, yes, yes, we must have this". Now though, I'm not so sure why I wanted it back then. It just feels too "clever", ie it sounds great in theory, but it's really hard in practice to understand what's going on. What's really odd to me is that I'm sure I was a big fan of this a year ago and you hated it then, but now the roles have reversed 😕 I especially struggle with the deconstruct idea. If I see I really want to like this though. And I have a nagging feeling that straight after posting this, I find a situation where I'll think "if only I could do splatting"... |
Beta Was this translation helpful? Give feedback.
-
It's just too structural for me: positional parameters are iffy enough without hoping that 2 sets of them line up. If something operates on a |
Beta Was this translation helpful? Give feedback.
-
Good catch! I've updated the post. |
Beta Was this translation helpful? Give feedback.
-
I kind of agree with much of the criticism here. There's a bit too much magic going on here that having an explicit "splat" operator might help clear up, but even then it's pretty complicated. If it applied to just tuples that might be more palatable as the number of situations where it could be ambiguous should be significantly reduced, but even then it might be difficult to ascertain what method is actually being called when dealing with overloads. At the end of the day it's probably not worth it, but it's fun to explore. |
Beta Was this translation helpful? Give feedback.
-
Splatting was suggested in the original Roslyn issue about tuples: dotnet/roslyn#347 I do think it would be better if it was explicit and that would also avoid ambiguities. |
Beta Was this translation helpful? Give feedback.
-
A splat operator may also help with more cases like arrays, tuples that aren't necessarily returned from a function so it can be expanded to more areas. Here are more cases where you might want to transform a tuple into an array and vice-versa. var a = new int[] { ...t } So in my opinion if we want to add something like a splat operator to the language it should go beyond unpacking tuples into argument list. |
Beta Was this translation helpful? Give feedback.
-
I like this feature; ever since I learned F# and realised that object.M (4, 5) and let args = 4, 5
object.M args are (at a high level) the same thing; it actually kinda blew my mind. Given the fact that, in F#, all functions have the same signature: In C#, however, I think this would feel inconsistent and incomplete unless we were also able to treat say public class ActionContainer<T>
{
private readonly Action<T> _action;
public ActionContainer(action) => _action = action;
public void Execute(T arguments)
{
_action(arguments);
}
}
var container = new ActionContainer<(string, object[])>(Console.WriteLine);
container.Execute("Arg1 {0}, arg2 {1}", new [] { "one", "two" }); In essence, I don't think that Tuple splatting on its own is very useful. |
Beta Was this translation helpful? Give feedback.
-
So, is it being proposed that we introduce support for this: class Program
{
static void Main(string[] args)
{
var arguments = (123, "Hello", DateTime.Now);
DoSomeWork(arguments);
}
static void DoSomeWork(int A, string B, DateTime C)
{
return;
}
} |
Beta Was this translation helpful? Give feedback.
-
Nemerle has this feature with implicitly expanding tuple. |
Beta Was this translation helpful? Give feedback.
-
C# method overloading is complicated. Probably too complicated. Obviously the best thing that we could do is complicate it some more!
I've been thinking about this a little since the discussion/debate/flame-war regarding "unit", that it would be neat if the lines between tuples and parameter lists could be blurred. Then when this proposal was published I began to wonder if it could apply to deconstructables in general.
So, to keep it short, what if the language could automatically consider a tuple/deconstructable value as arguments towards a method. Obviously this could only be considered if the tuple/deconstructable type wasn't already a valid argument.
Consider this 🍝 against the wall:
Splatting 101:
Mixing with arguments?
Flattening?
Tear it apart.
Beta Was this translation helpful? Give feedback.
All reactions