function return type feature request #808
Replies: 9 comments
-
What's wrong with directly returning right the value? public MyObj MyFunction()
{
if (SomethingIsTrue)
{
return new MyObj ();
}
return null;
} My primary issue with this is that it makes it too easy to forget to return a value: Today, if I write Also, what happens if something called |
Beta Was this translation helpful? Give feedback.
-
Make C# VB Again! |
Beta Was this translation helpful? Give feedback.
-
That's not even good practice in VB since VB.NET added a proper Return keyword. Remains mostly for legacy migration purposes. |
Beta Was this translation helpful? Give feedback.
-
How about public MyObj MyFunction() => SomethingIsTrue ? new MyObj() : null; ? I know, this is not applicable in the general case .... But what about the case (as @svick said), when a field/variable/function/.... is already called value ?
And is it that much effort to type the five characters after typing return ? ^^
|
Beta Was this translation helpful? Give feedback.
-
I actually like the idea the function already knows its return type so we don't have to declare the result. There was a proposal floating around to re-use the public MyObj MyFunction() {
if (SomethingIsTrue)
{
value = new MyObj ();
}
return value;
} also, in a case where we are dealing with lists, Intellisense should know to pop-up the types when pressing = on example below: public List<MyObj> MyFunction() {
value = new List<MyObj>();
if (SomethingIsTrue)
{
value.Add(new MyObj ());
}
return value;
} |
Beta Was this translation helpful? Give feedback.
-
What exactly would be returned in the first example? To remain consistent with the rest of the language that should be a compiler error as Public Function MyFunction() As Integer
If SomethingIsTrue Then
MyFunction = 1
End If
MyFunction = 2 ' oops!
End Function |
Beta Was this translation helpful? Give feedback.
-
A prototype is available at dotnet/roslyn with vbc.exe. Seriously though, the fact that this feature exists in VB for compat reasons clearly shows that it's not a good idea to be duplicated in C#. |
Beta Was this translation helpful? Give feedback.
-
@HaloFour The return type would be default(T) of the return type of function, implicitly assigned by the compiler before the function body. There can be plenty of examples where we want to result.Add(anotherValue) so returning immediately is not always an option. |
Beta Was this translation helpful? Give feedback.
-
If implicit return locals were ever added to C#, I would request a diagnostic and quick fix to replace it with clean code so that I could force the diagnostic to error level for every project. I wouldn't want anyone to accidentally use it. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Properties have this magic convention in the setter called "value" which is not explicitly typed in the set arguments.
Currently we need to explicitly define the return object with the function body
Functions already know the return type so there should be a similar convention with the magic "value"
Beta Was this translation helpful? Give feedback.
All reactions