Replies: 9 comments
-
All the other constructs are having an existential crisis because of these tuples. |
Beta Was this translation helpful? Give feedback.
-
I vote down only because I prefer the |
Beta Was this translation helpful? Give feedback.
-
I vote down as this conversion seems conflict with
I would prefre som spreading operator, like
|
Beta Was this translation helpful? Give feedback.
-
I found this interesting when there was an implicit fan out of the itemes of a value tuple to the arguments of a constructor. public Person(string name, string city) { ... }
var personTuple = ("Bill", "Redmond");
Person person = new Person(personTuple); In case of an existing constructor with a corresponding ValueTuple as parameter, that one takes precedence of course. |
Beta Was this translation helpful? Give feedback.
-
How should the feature interact with optional parameters? class Program
{
static void Main()
{
M(("", "")); // do you expect overload resolution to disambiguate it somehow?
}
static void M(Person1 p) {}
static void M(Person2 p) {}
}
class Person1 { public Person1(string s1, string s2) {} }
class Person2 { public Person2(string s1, string s2, string s3 = default) {} } How should it interact with type inference? class Person
{
static void Main()
{
M(new Person(), ("", ""));
M(() =>
{
if (new Random().Next() > 1)
return new Person();
return ("", "");
});
}
static void M<T>(T t1, T t2) {}
static void M<T>(Func<T> f) {}
public Person(string s1, string s2) { }
public static implicit operator (string s1, string s2) (Person p) => ("", "");
} Under the original proposal both calls will become ambiguous as both types are implicitly convertible to each other. That's a breaking change. Also the code will become brittle as the program above will rely on the presence of the constructor with two string arguments. Adding/removing the constructor or changing its signature will either make the code above behave differently or will make it ambiguous. |
Beta Was this translation helpful? Give feedback.
-
It doesn't conflict with The compiler already has rules for dealing with overloads and optional parameters. I'd expect that the same rules would be followed. And, as stated (not so explicitly) above, this conversion consideration would be a last ditch effort by the compiler. If there's an existing way to convert the tuple to the type that would be used instead. If a different overload would be considered due to the lack of a conversion, that overload would be invoked instead. The only time that this conversion would be considered is if otherwise the code would fail to compile. I do realize that this opens up other potential cans of worms, that's why this is a discussion.
Changing a constructor signature is already a breaking change and ambiguity is already a consequence of that. |
Beta Was this translation helpful? Give feedback.
-
What if constructor throws an exception? I think it is a big pitfall with this idea. Implicit cast should never throw an exception. |
Beta Was this translation helpful? Give feedback.
-
How about a binary operator for it? I wrote a parser some time ago with this functionality which used var arguments = ("Bill", "Redmond");
var person = new Person!arguments; |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I was reading through the comments on #676 and it made me wonder what benefits there might be in allowing tuples to be convertible to other arbitrary types if after exhausting all other options the compiler could match the tuple to a constructor of that type. Something like this:
The conversion would only be considered when:
Open questions:
I want to say that there was also a proposal on the Roslyn repo for something similar.
Beta Was this translation helpful? Give feedback.
All reactions