2

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?

1 Answers1

0

If I understand the relevant section of Scala Reference correctly, it's simply not allowed: the right-hand side of an infix operation can either be an expression or several expressions in parentheses, you can't have multiple argument lists.

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487