Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional invocation of a method in Scala

Tags:

scala

scalaz

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?

like image 491
Eric Avatar asked Sep 05 '11 23:09

Eric


People also ask

What is the meaning of => in Scala?

=> 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 .

Is there ternary operator in Scala?

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.


1 Answers

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.

like image 173
socom1880 Avatar answered Oct 27 '22 01:10

socom1880