0

I'm actually running this code in Scala

val obj2 = obj1.method1()
val result = obj2.method2(obj2.name)

I would know if is somehow possible use the obj2 in a argument concatenating the calls, to clarify I would write something like

val result = obj1.method1().method2(_.name)

where _ is referring to the object previously computed. I don't know if it's possible, it's just a style question, the first code works fine

Cattani Simone
  • 1,168
  • 4
  • 12
  • 30
  • @manub, I have placed the underscore because in Scala it's often use for this kind of thinks, and I would simply clarify what I would do, but actually is not the correct syntax and doesn't work, so I would know if could be possible obtain something like that – Cattani Simone Mar 09 '16 at 17:20

1 Answers1

3

We can do this (borrowing into name from http://combinators.info/):

implicit class Into[A](x: A) {
  def into(f: A => B) = f(x)
}

obj1.method1().into { x => x.method2(x.name) }
Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487