Proposal: Multiple possibilities return value #1891
Replies: 9 comments
-
So, if you have |
Beta Was this translation helpful? Give feedback.
-
What you are proposing is often implemented as Discriminated Unions, which is championed in #113. |
Beta Was this translation helpful? Give feedback.
-
Sounds like #399. I have a |
Beta Was this translation helpful? Give feedback.
-
@HaloFour |
Beta Was this translation helpful? Give feedback.
-
Syntax 1: Syntax 2: It is already valid today. What are you proposing? Syntax 3: Duplicate of #399. |
Beta Was this translation helpful? Give feedback.
-
This proposes a TypeScript idiom that makes a lot of sense in the dynamically typed world of JavaScript; it doesn't make any sense to me in C#. Shapes and Extensions (#164) would provide all of the benefits of this proposal, and a lot more, but in a form that's type safe and rigorous, as suits the nature of C#. |
Beta Was this translation helpful? Give feedback.
-
@hez2010 From what I understand, you can't overload on return types for very good reasons. It had to do with the compiler never being able to know which when they meant. All of these examples use your first syntax example. Examples: a(); // does string or int get called?
...
public void Foo(int ...) { ... }
public void Foo(string s) { ... }
...
Foo(a()) // which overload is called and which a is called?
...
object mysteryType = a(); // Which a gets called?
int x = mysteryType; // ...and will this work? It depends, is mysteryType an int or string? I don't know and neither does the compiler etc., I'm probably missing a lot here. In the meantime, just use |
Beta Was this translation helpful? Give feedback.
-
can you please give an example of how to use |
Beta Was this translation helpful? Give feedback.
-
The |
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 I want a function to return value of different types, I have to write like this:
or
I think the code below will be useful and can both constraint the type of returned value and reduce duplicated code:
Beta Was this translation helpful? Give feedback.
All reactions