Skip to content

Instantly share code, notes, and snippets.

@danieldietrich
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danieldietrich/95e92d9430b01d1019c1 to your computer and use it in GitHub Desktop.
Save danieldietrich/95e92d9430b01d1019c1 to your computer and use it in GitHub Desktop.
/**
* This is a Java class using Javaslang's Lazy
*/
class LazyDemo {{
// -- Lazy instance
Lazy<String> value = Lazy.of(() -> "Yay!");
value.toString(); // = Lazy(?)
value.get(); // = "Yay!"
value.toString(); // = "Lazy(Yay!)
// -- lazy value
// chars is a CharSequence Proxy backed by a Lazy
CharSequence chars = Lazy.asVal(() -> "Yay!", CharSequence.class);
// = "Yay!" (lazily evaluated)
chars.toString();
}}
#
# This is just a comparison to Scala's behavior
#
scala> lazy val s = "Yay!"
s: String = <lazy>
scala> println(s)
Yay!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment