<p>Say instead of returning void a method you returned a reference to the class even if it didn't make any particular semantic sense.  It seems to me like it would give you more options on how the methods are called, allowing you to use it in a fluent-interface-like style and I can't really think of any disadvantages since you don't have to do anything with the return value (even store it).</p>

<p>So suppose you're in a situation where you want to update an object and then return its current value.
instead of saying </p>

<pre><code>myObj.Update();
var val = myObj.GetCurrentValue();
</code></pre>

<p>you will be able to combine the two lines to say</p>

<pre><code>var val = myObj.Update().GetCurrentValue();
</code></pre>

<hr>

<p><strong>EDIT:</strong>  I asked the below on a whim, in retrospect, I agree that its likely to be unnecessary and complicating, however my question regarding returning this rather than void stands.</p>

<p>On a related note, what do you guys think of having the language include a new bit of syntactic sugar:</p>

<pre><code>var val = myObj.Update()&lt;.GetCurrentValue();
</code></pre>

<p>This operator would have a low order of precedence so myObj.Update() would execute first and then call  GetCurrentValue() on myObj instead of the void return of Update.</p>

<p>Essentially I'm imagining an operator that will say "call the method on the right-hand side of the operator on the first valid object on the left".  Any thoughts?</p>

[{'answer_id': 57165, 'author': 'argv0', 'author_id': 5595, 'author_profile': 'https://Stackoverflow.com/users/5595', 'pm_score': 2, 'selected': False, 'text': '<p>Returning "self" or "this" is a common pattern, sometimes referred to as <a href="http://www.martinfowler.com/dslwip/MethodChaining.html" rel="nofollow noreferrer">"method chaining"</a>.  As for your proposed syntax sugar, I\'m not so sure.  I\'m not a .NET guy, but it doesn\'t seem terribly useful to me.  </p>\n'}, {'answer_id': 57176, 'author': 'John Calsbeek', 'author_id': 5696, 'author_profile': 'https://Stackoverflow.com/users/5696', 'pm_score': 2, 'selected': False, 'text': '<p>The NeXTSTEP Objective-C framework used to use this pattern. It was discontinued in that framework once distributed objects (remote procedure calls, basically) were added to the language—a function that returned <code>self</code> had to be a synchronous invocation, since the distributed object system saw the return type and assumed that the caller would need to know the result of the function.</p>\n'}, {'answer_id': 57178, 'author': 'Steve Morgan', 'author_id': 5806, 'author_profile': 'https://Stackoverflow.com/users/5806', 'pm_score': 4, 'selected': False, 'text': "<p>I think as a general policy, it simply doesn't make sense. Method chaining in this manner works with a properly defined interface but it's only appropriate if it makes semantic sense. </p>\n\n<p>Your example is a prime one where it's not appropriate, because it makes no semantic sense.</p>\n\n<p>Similarly, your syntactic sugar is unnecessary with a properly designed fluent interface.</p>\n\n<p>Fluent interfaces or method chaining <b>can</b> work very well, but need to be designed carefully.</p>\n"}, {'answer_id': 57203, 'author': 'Mendelt', 'author_id': 3320, 'author_profile': 'https://Stackoverflow.com/users/3320', 'pm_score': 4, 'selected': True, 'text': "<p>I know in Java they're actually thinking about making this standard behaviour for void methods. If you do that you don't need the extra syntactic sugar.</p>\n\n<p>The only downside I can think of is performance. But that's easilly measured. I'll get back to you with the results in a few minutes :-)</p>\n\n<p>Edit:</p>\n\n<p>Returning a reference is a bit slower than returning void .. what a surprise. So that's the only downside. A few more ticks when calling your function.</p>\n"}, {'answer_id': 57212, 'author': 'Tim Frey', 'author_id': 1471, 'author_profile': 'https://Stackoverflow.com/users/1471', 'pm_score': 2, 'selected': False, 'text': "<p>The only disadvantage I can see is that it makes the API slightly more confusing.  Let's say you have some collection object with a remove() method that would normally return void.  Now you want to return a reference to the collection itself.  The new signature would look like:</p>\n\n<pre><code>public MyCollection remove(Object someElement)\n</code></pre>\n\n<p>Just looking at the signature, it's not clear that you're returning a reference to the same instance.  Maybe MyCollection is immutable and you're returning a new instance.  In some cases, like here, you would need some external documentation to clarify this.</p>\n\n<p>I actually like this idea, and I believe that there was some talk in retrofitting all void methods in Java7 to return a reference to 'this', but it ultimately fell through. </p>\n"}, {'answer_id': 57227, 'author': 'Pete', 'author_id': 76, 'author_profile': 'https://Stackoverflow.com/users/76', 'pm_score': 2, 'selected': False, 'text': '<p>Isn\'t this how "fluent interfaces" - like those that JQuery utilizes - are built?  One benefit is supposed to be code readability (though the wikipedia entry at <a href="http://en.wikipedia.org/wiki/Fluent_interface" rel="nofollow noreferrer">http://en.wikipedia.org/wiki/Fluent_interface</a> mentions that some find it <em>NOT</em> readable).  Another benefit is in code terseness, you lose the need to set properties in 7 lines of code and then call a method on that object in the 8th line.</p>\n\n<p>Martin Fowler (who coined the term here - <a href="http://martinfowler.com/bliki/FluentInterface.html" rel="nofollow noreferrer">http://martinfowler.com/bliki/FluentInterface.html</a>) says that there is more to fluent interfaces than method chaining, however method chaining is a common technique to use with fluent interfaces.</p>\n\n<p>EDIT:\nI was actually coming back here to edit my answer and add that there is no disadvantage to returning this instead of void in any measurable way, when I saw George\'s comment pointing out that I did forget to discuss the <em>point</em> of the question.  Sorry for the initial "pointless" rambling.</p>\n'}, {'answer_id': 57792, 'author': 'Ismael', 'author_id': 5999, 'author_profile': 'https://Stackoverflow.com/users/5999', 'pm_score': 0, 'selected': False, 'text': "<p>At first sight it may look good, but for a consistent interface you will need that all methods return a reference to this (which has it own problems).</p>\n\n<p>Let say you have a class with two methods GetA which return this and GetB which return another object:</p>\n\n<p>Then you can call obj.GetA().GetB(), but not obj.GetB().GetA(), which at least doesn't seems consistent.</p>\n\n<p>With Pascal (and Visual Basic) you can call several methods of the same object.</p>\n\n<blockquote>\n<pre><code>with obj\n  .GetA();\n  .GetB();\nend with;\n</code></pre>\n</blockquote>\n\n<p>The problem with this feature is that you easily can write code that is harder to understand than it should be. Also adding a new operator probably make it ever harder.</p>\n"}]
['https://Stackoverflow.com/questions/57140', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5056/']