Created
June 28, 2012 19:02
-
-
Save ClintCombs/3013226 to your computer and use it in GitHub Desktop.
Using Scala's Option from Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.ccombs.scalaFromJava; | |
import scala.Some; | |
import scala.Option; | |
import scala.None; | |
/** | |
* OptionExample | |
*/ | |
public class OptionExample { | |
/** | |
* Construct a Scala Option[T] from a given value. | |
*/ | |
private static final <T> scala.Option<T> option(final T value) { | |
return (value != null)?new Some<T>(value):scala.Option.apply((T) null); | |
} | |
public static void main(final String args[]) { | |
final Some<String> s1 = (Some<String>) option("s1"); | |
// this doesn't work | |
// final None n = (None) option(null); | |
final Option<String> n = option(null); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment