Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any way to get the return value of last method in cascade?

I'm doing something like this:

new A()
  ..methodA()
  ..methodB()
  .toString();

Should this return the result of toString()? Currently it's returning the new A object.

like image 749
Daniel Robinson Avatar asked Sep 12 '25 05:09

Daniel Robinson


1 Answers

In your code toString() is applied on the result of methodB(). It's like you are doing :

var func = (o) {
  o.methodA();
  o.methodB().toString();
  return o;
};
func(new A());

To do what you want, you have to do something like :

(new A()
  ..methodA()
  ..methodB()).toString();
like image 87
Alexandre Ardhuin Avatar answered Sep 16 '25 09:09

Alexandre Ardhuin