Chaining is an object-oriented programming technique where methods return the object on which they were called so that another method may be called on the same object, thus forming a method chain.
Questions tagged [chaining]
1000 questions
120
votes
5 answers
C++ execution order in method chaining
The output of this program:
#include
class c1
{
public:
c1& meth1(int* ar) {
std::cout << "method 1" << std::endl;
*ar = 1;
return *this;
}
void meth2(int ar)
{
std::cout << "method 2:"<< ar…

Moises Viñas
- 1,073
- 2
- 7
- 9
83
votes
3 answers
Javascript inheritance: call super-constructor or use prototype chain?
Quite recently I read about JavaScript call usage in MDC
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/call
one linke of the example shown below, I still don't understand.
Why are they using inheritance here like this…

Jeremy S.
- 6,423
- 13
- 48
- 67
73
votes
7 answers
Java 8 apply function to all elements of Stream without breaking stream chain
Is there a way in Java to apply a function to all the elements of a Stream without breaking the Stream chain? I know I can call forEach, but that method returns a void, not a Stream.

alexgbelov
- 3,032
- 4
- 28
- 42
59
votes
4 answers
How to achieve method chaining in Java?
I want to achieve method chaining in Java.
How can I achieve it?
Also let me know when to use it.
public class Dialog {
public Dialog() {
}
public void setTitle(String title) {
//Logic to set title in dialog
}
…

Bug
- 625
- 1
- 5
- 7
54
votes
7 answers
How to select an element's parent and the parent's siblings
I have this code:
$("#test").siblings('p').remove();
$("#test").remove();
How can I chain this code instead of writing it separately?
user2281332
46
votes
1 answer
Search on descendants of an element
With protractor whats the best way to select child elements? Say we have the layout below...
Red
Blue
Red

Brad8118
- 4,672
- 11
- 36
- 48
40
votes
6 answers
Method Chaining in Java
While answering a few questions on here earlier and from some work I have been doing lately I have been wondering why Java does not support method chaining on its built in classes.
If I were to create a Car class for example, I could make it…

Jon Taylor
- 7,865
- 5
- 30
- 55
33
votes
3 answers
Optional Chaining Operator in Typescript
In javascript, Optional Chaining Operator is supported by the babel plugin.
But I can't find how to do this in Typescript. Any idea?

cwtuan
- 1,718
- 1
- 18
- 20
31
votes
5 answers
How does basic object/function chaining work in javascript?
I'm trying to get the principles of doing jQuery-style function chaining straight in my head. By this I mean:
var e = f1('test').f2().f3();
I have gotten one example to work, while another doesn't. I'll post those below. I always want to learn the…

Geuis
- 41,122
- 56
- 157
- 219
28
votes
7 answers
Get detail messages of chained exceptions Java
I'd like to know how I could throw a "final" Exception, containing a detailed message with all the detailed messages of a number of chained exceptions.
For example suppose a code like this:
try {
try {
try {
try {
//Some error…

MikO
- 18,243
- 12
- 77
- 109
28
votes
4 answers
Is there a "nullsafe operator" in PHP?
Is there any way to write the following statement using some kind of safe navigation operator?
echo $data->getMyObject() != null ? $data->getMyObject()->getName() : '';
So that it looks like this:
echo $data->getMyObject()?->getName();

Dennis
- 1,027
- 1
- 14
- 30
26
votes
2 answers
Chain promises with AngularJS
I have a service called paymentStrategy that get injected in my controller.
$scope.buy = function() {
paymentStrategy.buy()
.then(function(response) {
}
}
This buy method from paymentStrategy triggers several methods that needs to be…

Florent Valdelievre
- 1,546
- 3
- 20
- 32
25
votes
6 answers
Go method chaining and error handling
I want to create a method chaining API in Go. In all examples I can find the chained operations seem always to succeed which I can't guarantee. I therefore try to extend these to add the error return value.
If I do it like this
package main
import…

johannes
- 15,807
- 3
- 44
- 57
25
votes
9 answers
Benefits and drawbacks of method chaining and a possibility to replace all void return parameters by the object itself
I am mostly interested in Java, but I think it's a general question. Recently I've been working with Arquillian framework (ShrinkWrap) that uses a lot of method chaining. Other example of method chaining are methods in StringBuilder, StringBuffer.…

MartinTeeVarga
- 10,478
- 12
- 61
- 98
24
votes
10 answers
conditional chaining in ruby
Is there a good way to chain methods conditionally in Ruby?
What I want to do functionally is
if a && b && c
my_object.some_method_because_of_a.some_method_because_of_b.some_method_because_of_c
elsif a && b && !c
…

DanSingerman
- 36,066
- 13
- 81
- 92