Search code examples
packagehaxenmenekomulti-targeting

Haxe: Import from neko, cpp and java sys trees


I want to use Haxe to write a library that can be used by other projects in various different languages.

Currently I have at the top of my library:

import neko.io.File;
import neko.io.FileInput;
import neko.io.FileOutput;
import neko.FileSystem;
import neko.io.Process;

So my library compiles to neko just fine, using the -neko flag. However if I try to use the -cpp flag, the packages cannot be found:

$ haxe -cp src -main sws/Root.hx -cpp build/sws.CXX
src/sws/Root.hx:3: characters 0-20 : You can't access the neko package with current compilation flags (for neko.io.File)

I thought the solution would be to instead do the imports like this:

import sys.io.File;
import sys.io.FileInput;
import sys.io.FileOutput;
import sys.FileSystem;
import sys.io.Process;

and let Haxe change sys into neko or cpp depending on the compile flag I use. (Assuming all the modules are available in all the target languages.) But that doesn't work either.

$ haxe -cp src -main sws/Root.hx -neko build/sws.n
src/sws/Root.hx:3: characters 0-19 : Class not found : sys.io.File
$ haxe -cp src -main sws/Root.hx -cpp build/sws.CXX
src/sws/Root.hx:3: characters 0-19 : Class not found : sys.io.File

How should I be doing it?


Solution

  • If import neko.io.File; works, you're probably using Haxe 2.x, not Haxe 3. (Unless I'm missing something?)

    In Haxe 3, you would use import sys.io.File etc. Migration notes for Haxe 3 can be found at: http://haxe.org/manual/haxe3/migration

    In Haxe 2, you had to do it per target. I would do things like:

    #if neko
        import neko.io.File;
        import neko.io.FileInput;
        import neko.io.FileOutput;
        import neko.FileSystem;
        import neko.io.Process;
    #elseif cpp
        import cpp.io.File;
        import cpp.io.FileInput;
        import cpp.io.FileOutput;
        import cpp.FileSystem;
        import cpp.io.Process;
    #end
    

    Assuming of course all those classes existed in the CPP target on your Haxe release.

    If not, maybe look at upgrading to Haxe 3 :)