There are so many horrible ways you could do this kind of thing in Scala:
sealed trait Action { def doIt(): Unit }
class InnerAction(message: String) extends Action { def doIt() = print(message) }
class WrapperAction(message: String, inner: Action) extends Action {
def doIt() = { inner.doIt(); print(s" inside $message") }
}
def foo(message: String)(implicit next: Action = null) =
Option(next).fold[Action](new InnerAction(message))(action =>
new WrapperAction(message, action)
)
trait MyNestType
implicit def actionToMyNestType(action: Action): MyNestType = {
action.doIt()
println()
new MyNestType {}
}
And then:
scala> val nested: MyNestType =
| foo("hi") {
| foo("bye") {
| foo("done")
| }
| }
done inside bye inside hi
nested: MyNestType = $anon$1@7b4d508f
Please don't ever do this, though. If you're writing Scala, write Scala.