2 ms·
This post contains several wrong statements. First, Java does support macros – you can use Annotation Processors normally (as Butterknife, Dagger, and other pr
by kuschku 10y ago
This post contains several wrong statements.
First, Java does support macros – you can use Annotation Processors normally (as Butterknife, Dagger, and other projects do), or you can use Annotation Processors to actually get the AST and modify it (not supported officially, but used by projects like Lombok).
Second, Java has no type classes – this is technically true, but not exactly. The mentioned example can also be handled with Java 8’s interfaces, as they support default methods and private or protected methods.
Due to that, you can define a method instead of the writes() field, and then use an interface-defined default method to handle actual serialization.
If combined with Annotation Processing, this allows easy, simple, compile-time generation of JSON serialization (and yes, I’ve written stuff like this before, so I know it’s actually usable, and not too complicated).