Skip to content

Instantly share code, notes, and snippets.

@rossabaker
Forked from chirino/gist:511342
Created August 6, 2010 14:50
Show Gist options
  • Save rossabaker/511426 to your computer and use it in GitHub Desktop.
Save rossabaker/511426 to your computer and use it in GitHub Desktop.
final class RichAnyRef[T <:AnyRef](val value: T) extends Proxy {
def self: Any = value
def ? : Option[T] = Option(value)
def ?[R] ( func: (T=>R) ) : Option[R] = Option(value) map func
}
implicit def RichAnyRefWrapper[T <:AnyRef](x: T) = new RichAnyRef(x)
final class RichOption[T](val value: Option[T]) extends Proxy {
def self: Any = value
def | (other:T): T = value.getOrElse(other)
}
implicit def RichOptionWrapper[T](x: Option[T]) = new RichOption(x)
var x = "hello"
x.?.|("not set")
x.?( _.length )
x = null
x.?.|("not set")
x.?( _.length )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment