Skip to content

Instantly share code, notes, and snippets.

@mmaz
Last active December 19, 2015 22:49
Show Gist options
  • Save mmaz/6030687 to your computer and use it in GitHub Desktop.
Save mmaz/6030687 to your computer and use it in GitHub Desktop.
scala Option to Guava Optional
name := "ScalaOptionToGuavaOptional"
scalaVersion := "2.10.2"
libraryDependencies += "com.google.guava" % "guava" % "14.0.1"
libraryDependencies += "com.google.code.findbugs" % "jsr305" % "2.0.1"

Option -> Optional

one-liner to make a Guava Optional from a scala Option (keep in mind that Guava Optionals are not monadic)

Todo

Implicit conversion example

import com.google.common.base.Optional
def toOptional[T >: Null](x : Option[T]) : Optional[T] = Optional.fromNullable[T](x.orNull[T])
//examples
toOptional(None)
toOptional(Some("dude"))
toOptional(Some(3))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment