Scala partial function literal

June 27, 2016
Scala

In Scala there’s no literal for partial function types. You have to give the exact signature of the PartialFunction

val pf: PartialFunction[Double, Double] = { case x if x >= 0 => Math.sqrt(x) }

Though I personally prefer to use type ascription bacause you can define and lift in the same place if needed.

val pf = { case x if x >= 0 => Math.sqrt(x) } : PartialFunction[Double, Double]

It’s unwieldy, but I’ve learned to live with it.

But lately someone (can’t remember who – sorry!) told me that you don’t need a literal because you can write your own type alias and use it with infix operator concise syntax.

type :=>[A, B] = PartialFunction[A, B]

val pf: Double :=> Double = { case x if x >= 0 => Math.sqrt(x) }

Single-file Play Framework application with Ammonite

September 18, 2016
Scala PlayFramework JVM

My favorite features of Scala – part I

July 29, 2015
Scala