定义:静态扩展 一个静态扩展允许伪装扩展已存在的类型而不用修改它们的源码。在Haxe中这是通过声明一个静态方法,第一个参数是要扩展的类型,然后带入定义类到上下文中就可以使用。 [warning] Definition: Static Extension A static extension allows pseudo-extending existing types without modifying their source. In Haxe this is achieved by declaring a static method with a first argument of the extending type and then bringing the defining class into context through using.
静态扩展是一个强大的工具,使得可以不用实际修改就对类型进行扩展。下面的示例展示用法:
Static extensions can be a powerful tool which allows augmenting types without actually changing them. The following example demonstrates the usage:
using Main.IntExtender;
class IntExtender {
static public function triple(i:Int) {
return i * 3;
}
}
class Main {
static public function main() {
trace(12.triple());
}
}
显然,Int并没有原生提供一个 triple 方法,然而这个程序编译并输出预期的 36 。这是因为调用 12.triple() 是转换到 IntExtender.triple(12) 。这有三个要求:
Clearly,Int does not natively provide a triple method,yet this program compiles and outputs 36 as expected. This is because the call to 12.triple() is transformed into IntExtender.triple(12). There are three requirements for this:
- 字面的值12和第一个参数都是已知的Int类型
- IntExtender类通过 using Main.IntExtender 带入到上下文
- Int本身没有 triple字段(如果它有,那个字段会优先于静态扩展)
- Both the literal 12 and the first argument of triple are known to be of type Int.
- The class IntExtender is brought into context through using Main.IntExtender.
- Int does not havea triple field by itself (if it had,that field would take priority over the static extension).
静态扩展通常认为是语法糖,事实也是如此,但是值得注意的是,它们可以对代码可读性有一个戏剧性影响:替代嵌套的调用形式如f1(f2(f3(f4(x)))),使用链式调用形式如x.f4().f3().f2().f1() 。
Static extensions are usually considered syntactic sugar and indeed they are, but it is worth noting that they can have a dramatic effect on code readability: Instead of nested calls in the form of f1(f2(f3(f4(x)))), chained calls in the form of x.f4().f3().f2().f1() can be used.
遵守前面讲述过的解析顺序(第3.7.3节),多个使用的表达式从底部到头部检查,而在每个模块的类型以及在每个类型中的字段从头至尾检查。使用一个模块(而不是一个模块中的特定类型,查看模块和路径(第3.7节))作为静态扩展会把所有它的类型带入上下文中。
Following the rules previously described in Resolution Order(Section3.7.3),multiple using expressions are checked from bottom to top, with the types within each module as well as the fields within each type being checked from top to bottom. Using a module (as opposed to a specific type of a module, see Modules and Paths (Section 3.7)) as static extension brings all its types into context.
在Haxe标准库中的一些类适用于静态扩展用法。下面的例子展示了StringTools的用法:
Several classes in the Haxe Standard Library are suitable for static extension usage. The next example shows the usage of StringTools:
using StringTools;
class Main {
static public function main() {
"adc".replace("d", "b");
}
}
String本身并没有替换功能, using StringTools 静态扩展提供了一个。通常,JavaScript输出很好的显式了这个转换:
While String does not have a replace functionality by itself, the using StringTools static extension provides one. As usual, the JavaScript output nicely shows the transformation:
Main.main = function() {
StringTools.replace("adc","d","b");
}
如下Haxe标准库中的类都是设计作为静态扩展使用:
The following classes from the Haxe Standard Library are designed to be used as static extensions:
StringTools:提供字符串的扩展功能,例如替换和去除空格。 Lambda:提供功能方法到可迭代对象。 haxe.EnumTools:提供类型信息功能到enum和它们的实例。 haxe.macro.Tools:为和宏有关的操作提供不同的扩展(查看 工具(第9.4节))
StringTools: Provides extended functionality on strings, such as replacing or trimming. Lambda: Provides functional methods on iterables. haxe.EnumTools: Provides type information functionality on enums and their instances. haxe.macro.Tools: Provides different extensions for working with macros (see Tools (Section 9.4)).
使用using
花絮:使用using 自从using关键字被添加到语言中,讨论某些 使用using的问题 或者 using的影响变得非常常见。在很多情况下的英语变得尴尬,所以手册的作者决定把这个使用实际的功能称呼:静态扩展。 [warning] Trivia: “using” using Since the using keyword was added to the language, it has been common to talk about certain problems with “using using” or the effect of “using using”. This makes for awkward English in many cases, so the author of this manual decided to call the feature by what it actually is: Static extension.
大意可能就是说,using是作为静态扩展的关键字存在。如果本节标题使用 using using 会造成混淆。