I defined the following class like scala.Predef.Ensuring
:
scala> :paste
// Entering paste mode (ctrl-D to finish)
implicit class Doing[A](val self: A) extends AnyVal {
def doing(body: => Unit): A = { body; self }
def doWhen(pred: A => Boolean)(body: => Unit): A =
doing{ if (pred(self)) body }
}
// Exiting paste mode, now interpreting.
defined class Doing
but doWhen
method doesn't work with infix notation.
scala> "abc" doWhen(_ == "abc") { println("do") }
<console>:13: error: missing parameter type for expanded function ((x$1) => x$1.$eq$eq("abc"))
"abc" doWhen(_ == "abc") { println("do") }
^
scala> "abc".doWhen(_ == "abc") { println("do") }
do
res1: String = abc
How to use doWhen
by infix style?