Backwards compatibility threads

what’s the best way to update my threaded haxelib for backwards compatibility?

before I imported the target specific Thread classes but they were deprecated in 4.0.0 for sys.thread.Thread.

#if neko
import neko.vm.Thread;
#elseif java
import java.vm.Thread;
#elseif cpp
import cpp.vm.Thread;
#end

//...elsewhere...

#if (neko || java || cpp)
_worker = Thread.create(sendThreaded);
#end

I’m assuming that #if (target.threaded) won’t work before 4.0.0, does that mean I should do the following?

#if (target.threaded)
import sys.thread.Thread;
#elseif neko
import neko.vm.Thread;
#elseif java
import java.vm.Thread;
#elseif cpp
import cpp.vm.Thread;
#end

//...elsewhere...

#if (target.threaded || neko || java || cpp)
_worker = Thread.create(sendThreaded);
#end

if so, is there a way to do something like this?

#if (target.threaded || neko || java || cpp)
#define THREADED
#end

//...elsewhere...
#if THREADED

if so, is there a way to do something like this?

You can put a --macro Flags.redefine() into your library’s extraParams.hxml

And then:

import haxe.macro.*;
class Flags {
  static function redefine() {
    if (Context.isDefined('target.threaded')) 
      Compiler.define('threaded');
  }
}

You’ll just have to be careful to make sure that macro to runs before anything else causes typing to start.

1 Like