Calling multiple functions with one instance in scala

is there any way i can achieve the next in scala

with new Car() { examineColor bargain(300) buy } 

instead

 val c = new Car() c.examineColor c.bargain(300) c.buy 
+4
source share
2 answers

How about this:

 scala> val c = new Car { | examineColor | bargain(300) | buy | } 

Or:

 scala> { import c._ | examineColor | bargain(300) | buy | } 
+10
source

Assuming that these methods ( examineColor , bargain and buy ) are called for their side effects and not for their return values, you can use a chain template in which each of these methods returns this , allowing you to write code like this:

 val c1 = new Car() c1.examineColor.bargain(300).buy 
+2
source

Source: https://habr.com/ru/post/1307873/


All Articles