Allow names of tuple values in method return type to be based on input tuple value names #8741
Replies: 8 comments
-
Beta Was this translation helpful? Give feedback.
-
I like this, but I'm worried about people using it "just in case" and contributing to situations like this: var fooTask = FooAsync();
var barTask = bar.DoAsync();
// ...
var x = await (fooTask, barTask);
// Foo result is called x.fooTask, bar result is called x.barTask Or: var x = await DownloadStringsAsync((fooUrl, barUrl));
// String results are called x.fooUrl and x.barUrl |
Beta Was this translation helpful? Give feedback.
-
@jnm2 , I am not sure I understand the first example. Can a tuple be awaited? Is this an upcoming C# feature? Regarding the second example, do you mean that people would write a method like this?
|
Beta Was this translation helpful? Give feedback.
-
@ymassad To make something awaitable, all you have to do is define a GetAwaiter extension method returning an awaiter-shaped type. https://github.com/dotnet/corefx/issues/16010 tracks adding it to the BCL, and @buvinghausen has very kindly provided a NuGet package in the meantime. Yes, I'd be afraid people would be asked to do that when writing methods like that. |
Beta Was this translation helpful? Give feedback.
-
@jnm2, I see your point. But I think that any feature can be also abused. One way to mitigate this is to enable something like this:
What this means is that the name of the value in the output tuple can contain both a static part and another part that comes from the name of the values from the input tuples. |
Beta Was this translation helpful? Give feedback.
-
This seems like a fairly complicated language feature for something that is unlikely to be that common. The tuple element names for any input values are not related to the tuple element names for any output values and there are so many edge cases here that I don't think it's worth it. |
Beta Was this translation helpful? Give feedback.
-
@HaloFour, I think that the scenarios where it would be used are for methods that would "modify" the input value. So basically, the output would be a modified version of the input. In this case, one would like to have the names in the modified input be the same names from the input. One concrete example is partial invocation. A method called PartiallyInvoke would take in a function whose input is a tuple of size n and return a function whose input is a tuple of size n - 1. In this case, it is desirable to have the names of the tuple values untouched in the returned function. |
Beta Was this translation helpful? Give feedback.
-
This is the code for the example I talked about in the previous comment:
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
If we have the following code:
, then the type of the
result
variable will be(int a, int b)
. What I would like to do is change the Increment method in a way that theresult
variable will be of type(int x, int y)
(based on the names in the input tuple).I know that these types are actually the same as far as the CLR is concerned. What I am asking here is related to C#, not the CLR.
Basically, I should be able to do something like this:
Here I am surrounding the tuple value names with square brackets (this is probably bad syntax, I am just showing an example), to say that the name of the first value of the tuple in the return type should be the same name of the tuple value in the input parameter. And the same for the second tuple value.
I think that basically this can be done by creating something similar to
TupleElementNamesAttribute
that can map indexes of tuple values in the output to indexes of tuple values in the input. Or maybeTupleElementNamesAttribute
can be used here and a special prefix can be used to distinguish simple named tuple values from indexed ones. For example, "$3" means that this tuple value should be named based on the third tuple value in the input. This is possible because "$3" is an invalid parameter name (or is it a valid one on the CLR level)?Beta Was this translation helpful? Give feedback.
All reactions