Skip to content

Instantly share code, notes, and snippets.

@Sciss
Created April 5, 2015 12:52
Show Gist options
  • Select an option

  • Save Sciss/be182d7cc2f21af2ef22 to your computer and use it in GitHub Desktop.

Select an option

Save Sciss/be182d7cc2f21af2ef22 to your computer and use it in GitHub Desktop.
object ? {
implicit def some[A](x: A): ? [A] = new ? (Some(x))
implicit def wrap[A](opt: Option[A]): ? [A] = new ? (opt)
implicit def unwrap[A](x: ? [A]): Option[A] = x.option
}
final case class ? [A](private val option: Option[A]) extends AnyVal {
override def toString = option.toString
def ? [B] (fun: A => B) : Option[B] = option map fun
def !! : A = option.get
}
implicit class Elvis[A1](x: => A1) {
def ?: [A <: A1](opt: Option[A]): A1 = opt getOrElse x
}
var b: ?[String] = "abc"
b.?(_.length)
val l = b.?(_.length) ?: -1
b.!!.length()
b = None
val m = b.?(_.length) ?: -1
b.!!.length()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment