I've found this pattern quite a few times in my code:
if (doIt)
object.callAMethod
else
object
I'm wondering if there could be a syntactically more pleasing way to write the code above, especially to avoid the repetition of the object
variable. Something like:
// using the Scalaz "pipe" operator
// and "pimping" f: T => T with a `when` method
object |> (_.callAMethod).when(doIt)
Unfortunately the line above fails because the type inference requires a parameter type for (_.callAMethod)
.
My best approach for now is this:
implicit def doItOptionally[T](t: =>T) = new DoItOptionally(t)
class DoItOptionally[T](t: =>T) {
def ?>(f: T => T)(implicit doIt: Boolean = true) =
if (doIt) f(t) else t
}
implicit val doIt = true
object ?> (_.callAMethod)
Not great because I have to declare an implicit val
but this pays off if there are several chained calls:
object ?> (_.callAMethod) ?> (_.callAnotherMethod)
Does anyone have a better idea? Am I missing some Scalaz magic here?
=> is syntactic sugar for creating instances of functions. Recall that every function in scala is an instance of a class. For example, the type Int => String , is equivalent to the type Function1[Int,String] i.e. a function that takes an argument of type Int and returns a String .
Unlike Java, Scala does not have a ternary operator to write terse if-else expressions. As Scala's if expressions always return a value, having a ternary operator would be redundant.
I can't comment on your answer @Rex Kerr but a more concise way to do that would be:
implicit class When[A](a: A) {
def when(f: A => Boolean)(g: A => A) = if (f(a)) g(a) else a
}
Just putting the implicit
before the class allows you to omit the implicit function entirely.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With