Proposal: Override return in function calls #1342
Replies: 11 comments
-
Here is mine: obj.Bar(); obj.Baz(); obj.Womp(); Pow(obj); // vs
Pow(obj.Bar(return obj).Baz(return obj).Womp(return obj));
There's already a way to make things one line. And it has the virtue of being even shorter, even simpler, even more understandable, and can be done today without any language changes :) |
Beta Was this translation helpful? Give feedback.
-
It is one line, but it's not one statement, and that can matter. if (obj.Bar(return obj).Equals(obj2))
obj2 = obj.Baz(return obj).Womp(return obj).Quack(obj2);
// vs
obj.Bar();
if (obj.Equals(obj2)) {
obj.Baz(); obj.Womp();
obj2 = obj.Quack(obj2);
} |
Beta Was this translation helpful? Give feedback.
-
I don't see anything wrong with teh second form :) If you have long and complex functionality you want to encapsulate as an expression, then extracting an extension, or making a local function, seems like it would do nicely to solve the problem. |
Beta Was this translation helpful? Give feedback.
-
Personally I don't understand the desire to turn everything into a single-line expression. Putting that aside, I also don't follow the suggested syntax. Where exactly is this "return override" supposed to appear within the parameter list when the method actually accepts parameters, especially |
Beta Was this translation helpful? Give feedback.
-
If As for where it appears, it wouldn't actually be in the parameter list. Here's an example: public int Foo(string s, params int[] nums){
int val = 0;
// Something
return val;
}
int q = Foo("Test", x, 5, 2, 12, 555, returns x); |
Beta Was this translation helpful? Give feedback.
-
Isn't this basically I agree with HaloFour, I don't know what the deal is with this recent trend of this issues section being bombarded by people who think this is Perl, but this isn't Perl, it's C#, a language with a philosophy of writing code that's purposefully mildly-verbose for the sake of readability and clarity. |
Beta Was this translation helpful? Give feedback.
-
I'm completely confused as to how this proposal works for your last example:
Is |
Beta Was this translation helpful? Give feedback.
-
Whatever |
Beta Was this translation helpful? Give feedback.
-
And what does int One() => 1;
var x = 2;
var y = One(return x); After the last line is called, what would the value of |
Beta Was this translation helpful? Give feedback.
-
That would set both x and y to 2. I'm going to concede this proposal as others' arguments are compelling, but I'll leave it open for further discussion. |
Beta Was this translation helpful? Give feedback.
-
Isn't it a lot better to have an actual method chaining syntax? Although, now that I think about it, I think that's been turned down in the past. Examples might have been: obj.Foo()
..Bar()
..Baz(); |
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.
-
These might be existing extension functions to make code cleaner:
Which allows one to write:
Instead of
Making all of those extensions is really annoying though, and it can also be somewhat misleading. Typically, a function that returns a modified version of the
this
object will return a new instance of the object, whereas those extension functions just modify it. It's not necessarily intuitive and it may require a lot of repetitive code, either in the extensions or in the call. There should be a better way.So here is my proposed method of making one-line code in an understandable way:
By using the return keyword at the end of a function call, it would ignore the return value of the function (including void), and that call would instead return whatever object was put in that call.
An alternative to
return
could bereturns
, though that isn't an existing keyword so I don't know if that would cause any issues. Thereturns
keyword shouldn't cause issues with back-compatibility, though maybe I'm missing something there.Beta Was this translation helpful? Give feedback.
All reactions