Extren可以用一个类型安全的方式来描述目标语言特定的交互。它们像普通类一样定义,除了:
Externs can be used to describe target-specific interaction in a type-safe manner. They are defined like normal classes, except that
- class关键字被extern 关键字领先,
- 方法(第4.3节)没有表达式,
- 所有的参数和返回类型需要是显式的。
- the class keyword is preceded by the extern keyword,
- methods (4.3) have no expressions and
- all argument and return types are explicit.
常见的例子在Haxe标准库(第10章),Math 类,摘录如下:
A common example from the Haxe Standard Library (10) is the Math class, as an excerpt shows:
extern class Math
{
static var PI(default,null) : Float;
static function floor(v:Float):Int;
}
我们看到,externs 既可以定义方法也可以定义变量(时尚,PI是声明为一个只读属性(第4.2节))。一旦这个信息对于编译器可用,它启用相应的字段访问,也被称为类型:
We see that externs can define both methods and variables (actually, PI is declared as a readonly property (4.2)). Once this information is available to the compiler, it allows field access accordingly and also knows the types:
class Main {
static public function main() {
var pi = Math.floor(Math.PI);
$type(pi); // Int
}
}
这这可以运行,因为floor方法返回类型声明为Int。
This works because the return type of method floor is declared to be Int.
Haxe标准库带有许多外部类,对于Flash和JavaScript目标语言。它们使可以以类型安全的方式访问原生的APIs,当作设计高层APIs的工具。Externs也可以应用haxelib(第11章)中许多流行的原生库。
The Haxe Standard Library comes with many externs for the Flash and JavaScript target. They allow accessing the native APIs in a type-safe manner and are instrumental for designing higher-level APIs. There are also externs for many popular native libraries on haxelib (11).
Flash,Java和C#目标语言允许通过命令行(第7章)直接包括原生的库。目标特定的细节在每个目标语言细节(第12章)等各自的章节。
The Flash, Java and C# targets allow direct inclusion of native libraries from command line (7). Target-specific details are explained in the respective sections of Target Details (Chapter 12).
一些目标语言如Python或者JavaScript可能需要产生附加的 import 代码,为原生模块加载一个extern 类。Haxe提供声明这样的依赖关系在各自的目标语言向细节(第12章)的方式。。
Some targets such as Python or JavaScript may require generating additional ”import” code that loads an extern class from a native module. Haxe provides ways to declare such dependencies also described in respective sections Target Details (Chapter 12).
Haxe3.2.0以后剩余的参数和类型选择
Rest arguments and type choices Since Haxe 3.2.0
haxe.extern 包提供两种类型帮助映射原生的语义到Haxe:
The haxe.extern package provides two types that help mapping native semantics to Haxe:
Rest 这个类型可以被实用为一个最终的函数参数,来使传递一个额外的调用参数任意的数值到函数。类型参数可以被使用来限制这些参数为某个特定类型。
Rest: This type can be used as a final function argument to allow passing an arbitrary number of additional call arguments. The type parameter can be used to constrain these arguments to a specific type.
EitherType:这个类型允许使用任何一个类型参数类型,因此表示了一个类型选择。它可以被嵌套来允许更多的类型。
EitherType: This type allows using either of its parameter types,thus representing a type choice. It can be nested to allow more than two different types.
我们在下面代码示例中演示了其用法:
We demonstrate the usage in this code sample:
import haxe.extern.Rest;
import haxe.extern.EitherType;
extern class MyExtern {
static function f1(s:String, r:Rest<Int>):Void;
static function f2(e:EitherType<Int, String>):Void;
}
class Main {
static function main() {
MyExtern.f1("foo", 1, 2, 3); // use 1, 2, 3 as rest argument
MyExtern.f1("foo"); // no rest argument
//MyExtern.f1("foo", "bar"); // String should be Int
MyExtern.f2("foo");
MyExtern.f2(12);
//MyExtern.f2(true); // Bool should be EitherType<Int, String>
}
}