Just one import to access Javaslang
With Javaslang 2.0.0 we introduced javaslang.API, the best way to use language-level constructs like the pattern matching API and for-comprehensions.
import static javaslang.API.*;
class Example {
String stringify(int n) {
return Match(n).of(
Case(0, "zero"),
Case(1, "one"),
Case($(i -> i > 1), "many"),
Case($(), "negative")
);
}
}
Starting with 2.1.0 we will have more API methods. These give us access to nearly all Javaslang types. Here are some examples:
// instead of System.out.println
println("Finally!");
// instead of using null (TODO will throw NotImplementedError)
String html = TODO("load http://javaslang.io");
// instead of Tuple.of(1, "Hello")
Tuple(1, "Hello")
// same as HashMap.ofEntries(Tuple.of(1, "one"), Tuple.of(2, "two"))
Map(1, "one", 2, "two");
// instead of List.of(1, 2, 3)
List(1, 2, 3);
// instead of Option.some(1) and Option.none()
Some(1);
None();
*) Creating Maps will be safe, we removed the Map.of(Object...) method.
The only thing we have to do is adding one import:
import static javaslang.API.*;
Just a thought...
Wouldn't it be great if we were able to automatically have this import either in the scope of a package or if it could be defined as project default?
We could use the existing package-info.java or module-info.java files to define package-scoped or global imports. Of course javac would need to take these import statements into account.
Example:
File package-info.java:
/**
* Javaslang can be used throughout this package
* without the need to explicitly import it.
*/
package w00t;
import static javaslang.API.*;
File Test.java:
package w00t;
interface Test {
static void main(String[] args) {
println(Vector(args).mkString(", "));
}
}
But for now we still need to import javaslang.API in our classes...
Have fun!
- Daniel