And your example shows that there is no way to verify that each module provides a consistent API.
Module-level distinctions are per-target. It doesn't get any more clean and clear than that, does it? The trade off is that it's a bit limiting. Conditional compilation is where you can start to pick and choose targets/features and handle more sophisticated cases.
> And your example shows that there is no way to verify that each module provides a consistent API.
This is easy to do by providing a common interface definition and a simple build class that targets each language you're interested in. It's a good idea in a formal testing suite for a complex library.
I only do that in a handful of projects though, most of the time cross-target code is just a few special exceptions. In that case, you'd call out the special case, and then use the #else directive for the rest:
#if java
public inline function print(s : String) java.lang.System.out.println(v);
#else
public inline function print(s : String) Sys.println(s); // most of the time basic ops are covered by std lib.
#end
FWIW I've "inlined" things here with the keyword. This lets you alias cross-platform methods like this without extra function call overhead.
For single target code, you can prefix the class module with the platform tag (e.g. "js" or "java"). These module names are reserved within the compiler and any code therein is restricted to their respective targets.
Another way is to use conditional compilation markers to indicate specific target-compatible boundaries. E.g.
That method only exists on the java target, and the compiler will complain otherwise.Conditional compilation also allows for more fine-grained support of multiple targets:
This makes a cleaner organizational distinction between extern code that must remain target specific, and true cross platform code that can be called on any of the supported targets. Editors can pick up on this very easily and provide early warnings as you are typing, and doc generators can also inspect these directives and show api availability between targets in the rendered docs.(self-disclosure: Haxe compiler contributor)