6.3.2 Static Extension Metadata

since Haxe 4.0.0
Exclusion from Static Extension

By default, using a class as a static extension brings all of its static methods into the context, allowing them to be used as extensions of the appropriate types. In certain situations, the class can provide other static methods which are not intended for static extension. To make sure they do not interfere with the proper methods of the type, these methods can be marked with @:noUsing:

using Main.IntExtender;

class IntExtender {
  @:noUsing static public function double(i:Int) {
    return i * 2;
  }
  static public function triple(i:Int) {
    return i * 3;
  }
}

class Main {
  static public function main() {
    // works:
    trace(12.triple());
    // does not work because the method is marked with @:noUsing:
    //trace(12.double());
    // works as a normal static method:
    trace(IntExtender.double(12));
  }
}
Default Static Extension

It is also possible to always enable particular static extensions for a given type, by annotating the type with the @:using(args...) metadata. The arguments are the full dot paths of static extension classes that will be applied on the type:

@:using(Main.TreeTools)
enum Tree {
  Node(l:Tree, r:Tree);
  Leaf(value:Int);
}

class TreeTools {
  public static function sum(tree:Tree):Int {
    return (switch (tree) {
        case Node(l, r): sum(l) + sum(r);
        case Leaf(value): value;
      });
  }
}

class Main {
  static public function main() {
    var a = Node(Node(Leaf(1), Leaf(2)), Leaf(3));
    // works, even though there was no 'using Main.TreeTools' in this module
    trace(a.sum());
  }
}