Replies: 25 comments
-
That's too much magic, IMO. I'd make it a warning if there's a tuple/parameter name mismatch.
No. Arrays are homogeneous, tuples are heterogeneous. Those that aren't are a special case, not a rule.
Double no. Why not use an extension method for that if homogeneous tuples are really common in the codebase?
Structural typing, but only for tuples? Why not special case it for things where we have language constructs that presume the existence of certain members (LINQ,
Maybe, but why not use a special operator for cases like this? One will be "splat", the other one "name-splat". Then it can also be used when splatting tuples into argument lists. |
Beta Was this translation helpful? Give feedback.
-
Agreed. but making it a warning might be annoying if the tuple comes from an override parameter.
If we simply expand tuple items to the argument list that would be automatically supported.
Because you will need an extension method per each tuple length.
I think it doesn't mean "structural typing" exactly, the point is to select a member from all elements, like what
I'd rather keep this simple as possible and I would gladly exclude every case that seems too magical, like the first one above. |
Beta Was this translation helpful? Give feedback.
-
Is tuple projection really that common that we need special syntax that is more convenient than I'd rather lobby broadcasting from Julia. |
Beta Was this translation helpful? Give feedback.
-
@orthoxerox Unless we have structural typing, it's not clear what is the type of |
Beta Was this translation helpful? Give feedback.
-
@alrz I'm not proposing to reuse the syntax. What I tried to say was that enumerable projection is surely much more common than tuple projection, and we have to use |
Beta Was this translation helpful? Give feedback.
-
I think the whole point of the discussion is to avoid that kind of code. 😄 |
Beta Was this translation helpful? Give feedback.
-
I still feel that tuples are the wrong tool for the job here. If there are multiple tasks, why put them in a tuple? Just to save on array allocation? |
Beta Was this translation helpful? Give feedback.
-
Not if the |
Beta Was this translation helpful? Give feedback.
-
@alrz Then I'd rather wait for shapes. |
Beta Was this translation helpful? Give feedback.
-
@orthoxerox I wonder how would that look like from what I've seen in #164. Can you give an example? |
Beta Was this translation helpful? Give feedback.
-
@alrz You were right to doubt me, this will require HKT support: public shape SAwaitable<T>
{
public T Result {get;}
public TAwaiter<T> GetAwaiter() where TAwaiter<T> : SAwaiter<T>;
...
}
//witnesses omitted
public static (R1, R2, R3) Result<T1<R1>, T2<R2>, T3<R3>>(
this (T1<R1>, T2<R2>, T3<R3>) tuple)
where T1<R1> : SAwaitable<R1>
where T2<R2> : SAwaitable<R2>
where T3<R3> : SAwaitable<R3>
{
return (tuple.Item1.Result, tuple.Item2.Result, tuple.Item3.Result);
} |
Beta Was this translation helpful? Give feedback.
-
For the dictionary scenario, I think add an implicit convert from tuple to |
Beta Was this translation helpful? Give feedback.
-
Splatting is great for passing returned tuples to methods that don't consume tuples, all within an expression context. This makes sense if your primary use of tuples is for multiple return values. Having to always drop out to a statement context to deconstruct the tuple is annoying. (int x, int y) GetPosition() { ... }
void MoveTo(int x, int y) { ... } With splatting MoveTo(GetPosition()); Without splatting var p = GetPosition();
MoveTo(p.x, p.y); or var (x, y) = GetPosition();
MoveTo(x,y); |
Beta Was this translation helpful? Give feedback.
-
My argument is that it should be explicit to be applicable to other constructs like collection/array initializers. It being implict can be ambigious or confusing in some cases. |
Beta Was this translation helpful? Give feedback.
-
I love the idea, but I don't like the suggested syntax with the two dots. |
Beta Was this translation helpful? Give feedback.
-
Does splatting really require extra syntax? I know it's going to complicate overload resolution slightly, by not simply allow: public void Foo(int x, int y)
{
Console.WriteLine($"x: {x}, y: {y}");
}
var args = (4, 5);
Foo(args); If there was, indeed, another method with the following signature: public void Foo((int x, int y) t)
{
// ...
} Then that would obviously be a better overload match. FYI, this is exactly what F# does so there's precedent. |
Beta Was this translation helpful? Give feedback.
-
Could this be extended to allow a method to accept tuples of arbitrary width and return the projection? I want to be able to write one method which implements this pattern so that I don’t need to do this all over: await Task.WhenAll(new Task[] { tupleOfTasks.., });
var tupleOfResults = tupleOfTasks..Result; to be something more like: var tupleOfResults = await ResolveTupleAsync(tupleOfTasks); The current splatting syntax doesn’t seem to encourage reusable code unless if there is somehow a way to make a method accept arbitrary tuple lengths and return likewise. If I were to implement |
Beta Was this translation helpful? Give feedback.
-
JavaScript uses var tuple = (a: 1, b: 2);
dictionary.Add(...tuple); // dictionary.Add(tuple.a, tuple.b); |
Beta Was this translation helpful? Give feedback.
-
@Igorbek I think it is called "spread" operator |
Beta Was this translation helpful? Give feedback.
-
of course! sorry typo. fixed |
Beta Was this translation helpful? Give feedback.
-
Is this predated by #1654 ? |
Beta Was this translation helpful? Give feedback.
-
They are similar and related, but not the same. Also, this issue predates #1654 by over a year. |
Beta Was this translation helpful? Give feedback.
-
@yaakov-h Yeah, I think I misuse the word predate. I meant that this issue was made outdated by 1654 |
Beta Was this translation helpful? Give feedback.
-
@Thaina superceded? If so, no. This issue discussed explicitly splatting tuples. The other one discuses having the compiler magically splat tuples implicitly. |
Beta Was this translation helpful? Give feedback.
-
I would love that feature :-) When I use tuples and extract methods, they often end up with many braces and unnecessary names in the definition.
|
Beta Was this translation helpful? Give feedback.
-
Method arguments:
params parameters (we simply expand tuple elements to argument list, so this is not a special case):
Collection initializers:
Member access (binds to the member with the same name of each element, regardless of type):
Tuple elements (names will be reserved):
Open question: should we turn element names to argument names?(probably not)Array of a common type(probably not):Open question: should we allow splatting to anonymous types?(probably not because tuple names are optional)Beta Was this translation helpful? Give feedback.
All reactions