Welcome to Halva - Idiomatic Scala... in Java!
Halva's goal is to bring as many features from Scala to Java as is possible without byte code generation or magic. Using the features in Java should be as close as possibile to how the features are used in Scala.
I wanted to see how close to Scala I could get in Java. Not only close in functionality, but close in syntax as well. No magic, no byte code generation, nothing (too) tricky or magical. Just pure, standard Java 8. Halva is the result. The only reflection is for Google Guice's TypeLiteral class. The only "special" stuff is an annotation processor to generate case classes, type aliases and implicits. There are no dependencies whatsoever (Google Guice's TypeLiteral and JavaPoet are shaded into the code).
There are many 3rd party libraries that add support to Java in some form or another for some of Scala's features. However, most of these libraries are large and use their own DSLs or syntax. I want, where possible, to take unmodified Scala (adding semicolons!!) and get it to work in Java. Of course, this isn't literally possible. But, I began to wonder how close I could actually get. Some features are pretty trivial: some simple "sugaring" that wouldn't be hard to add. Other features, like Tuples, could be supported just as well in Java as in Scala. There are, however, a few features that are complicated or seemingly impossible in Java: Case Classes, Pattern Matching/Extractors/Partials, Comprehensions, and Implicits. There's no way to get total compatibility with Scala. But, could I get 50%? 70%? 80%? I searched the net for what people think are the killer features of Scala and attempted to implement them in pure Java. Project Halva is the result.
To see the results of what Halva can do, please look at the Simple Interpreter Example.
- Case Classes and Case Objects
- Pattern Matching and Extraction
- For Comprehensions
- Implicits
- Type Aliases
- Tuples
- Constructor Sugars
- Type Containers
- Custom Monadic Fors
- Anys
Many example generated files are shown here.
Halva is available from Maven Central. Use your favorite build tool and specify:
GroupId | ArtifactId | Description |
---|---|---|
io.soabase.halva | halva-processor | Contains the javac processor for @CaseClass, @CaseObject, @TypeAlias, @ImplicitClass, and @TypeContainer |
io.soabase.halva | halva | All the runtime code for Halva: matchers, comprehensions, etc. |
See miscellaneous info and Frequently Asked Questions
Special thanks to @Alex-At-Home for his help and great ideas.