Skip to content

Latest commit

 

History

History
204 lines (153 loc) · 7.38 KB

6.语言特性.md

File metadata and controls

204 lines (153 loc) · 7.38 KB

6.语言特性

抽象类型(第2.8节): 一个抽象类型是一个编译时构造,在运行时以一个不同的方式表示。这允许给存在的类型一个全新的意义。

Abstract types (2.8): An abstract type is a compile-time construct which is represented in a different way at runtime. This allows giving a whole new meaning to existing types.

外部类(第6.2节): 外部类可以被用于以一个类型安全的方式描述目标语言特定的交互。

Extern classes (6.2): Externs can be used to describe target-specific interaction in a type-safe manner.

匿名结构(第2.5节): 数据可以被简单的组织为匿名结构,减少小型数据类的必要性。

Anonymous structures (2.5): Data can easily be grouped in anonymous structures, minimizing the necessity of small data classes.

var point = { x: 0, y: 10 }; 
point.x += 10; 

数组推导(第6.6节): 使用 for 循环和一些逻辑快速创建和填充数组。

Array Comprehension (6.6): Create and populate arrays quickly using for loops and logic.

var evenNumbers = [ for (i in 0...100) if (i\%2==0) i ]; 

类,接口和继承(第2.3节): Haxe允许用类组织代码,使其成为一个面向对象语言。通常相关的功能如Jave等语言所支持的,包括继承和接口。

Classes, interfaces and inheritance (2.3): Haxe allows structuring code in classes, making it an object-oriented language. Common related features known from languages such as Java are supported, including inheritance and interfaces.

条件编译(第6.1节): 条件编译允许根据编译参数编译特定的代码。这有助于抽象目标语言特定的差异,但是也可以用于其他的目的,如更详细的调试。

Conditional compilation (6.1): Conditional Compilation allows compiling specific code depending on compilation parameters. This is instrumental for abstracting target-specific differences,but can also be used for other purposes, such as more detailed debugging.

\#if js 
    js.Browser.alert("Hello");
\#elseif sys 
    Sys.println("Hello"); 
\#end 

(广义的)代数数据类型(第2.4节): 结构可以通过代数数据类型(ADT)描述,如Haxe语言中的枚举。除此之外,Haxe支持它们的广义的变体如GADT。

(Generalized) Algebraic Data Types (2.4): Structure can be expressed through algebraic data types (ADT), which are known as enums in the Haxe Language. Furthermore, Haxe supports their generalized variant known as GADT.

enum Result { 
    Success(data:Array<Int>); 
    UserError(msg:String); 
    SystemError(msg:String, position:PosInfos); 
}

内联调用(第4.4.2节): 函数可以被设计为内联,使它们的代码直接插入调用的位置。通过手动的内联不用使代码重复这可以产生显著的效能提升。

Inlined calls (4.4.2): Functions can be designated as being inline, allowing their code to be inserted at call-site. This can yield significant performance benefits with out resorting to code duplication via manual inlining.

迭代器(第6.7节): 迭代一组值,例如一个数组的元素,在Haxe中可以很容易的迭代。定制类可以快速的实现迭代器功能来允许迭代。

Iterators (6.7): Iterating over a set of values, e.g. the elements of an array, is very easy in Haxe courtesy of iterators. Custom classes can quickly implement iterator functionality to allow iteration.

for (i in [1, 2, 3]) { 
    trace(i); 
} 

局部函数和闭包(第5.11节): Haxe中的函数不限于类字段,并可以被声明为表达式,允许强大的闭包。

Local functions and closures (5.11): Functions in Haxe are not limited to class fields and can be declared in expressions as well, allowing powerful closures.

var buffer = ""; 
function append(s:String) { 
    buffer += s; 
} 
append("foo"); 
append("bar"); 
trace(buffer); // foobar 

元数据(第6.9节): 添加元数据到字段,类或者表达式。这可以和编译器、宏,或者运行时的类沟通信息。

Metadata (6.9): Add metadata to fields, classes or expressions. This can communicate information to the compiler, macros, or runtime classes.

class MyClass { 
    @range(1, 8) var value:Int; 
} 
trace(haxe.rtti.Meta.getFields(MyClass).value.range); // [1,8] 

静态扩展(第6.3节): 存在的类和其它类型可以被额外的功能来扩展,通过使用静态扩展。

Static Extensions (6.3): Existing classes and other types can be augmented with additional functionality through using static extensions.

 using StringTools; 
 " Me & You ".trim().htmlEscape(); 

字符串插值(第6.5节): 字符串通过一个单引号声明,可以在当前的上下文访问变量。

String Interpolation (6.5): Strings declared with a single quotes are able to access variables in the current context.

trace(’My name is $name and I work in ${job.industry}’); 

偏函数应用(第6.8节): 任何函数可以应用为局部的,提供某些参数的值,然后保留其它的作为之后的字段。

Partial function application (6.8): Any function can be applied partially, providing the values of some arguments and leaving the rest to be filled in later.

var map = new haxe.ds.IntMap(); 
var setToTwelve = map.set.bind(_, 12);
setToTwelve(1);
setToTwelve(2); 

模式匹配(第6.4节): 复杂的结构可以被根据模式来匹配,从一个枚举或者一个结构中提取信息,并对特定的值组合定义特定的操作。

Pattern Matching (6.4): Complex structures can be matched against patterns, extracting information from an enum or a structure and defining specific operations for specific value combination.

var a = { foo: 12 }; 
switch (a) { 
    case { foo: i }: trace(i); 
    default: 
} 

属性(第4.2节): 变量类字段可以被涉及为属性,通过定制的read和write访问,可以更精细的访问控制。

Properties (4.2): Variable class fields can be designed as properties with custom read and write access, allowing fine grained access control.

public var color(get,set); 
function get_color() { 
    return element.style.backgroundColor; 
} 
function set_color(c:String) { 
    trace(’Setting background of element to $c’); 
    return element.style.backgroundColor = c; 
} 

访问控制(第6.10节): 访问控制语言特性使用Haxe元数据语法来禁止或者允许访问类或者字段。

Access control (6.10): The access control language feature uses the Haxe metadata syntax to force or allow access classes or fields.

类型参数、约束和变异(第3.2节): 类型可以通过类型参数来参数化,使类型化的容器和其它复杂的数据结构可用。类型参数也可以被约束为某些类型并遵守变异规则。

Type Parameters, Constraints and Variance (3.2): Types can be parametrized with type parameters, allowing typed containers and other complex data structures. Type parameters can also be constrained to certain types and respect variance rules.

 class Main<A> { 
    static function main() { 
        new Main<String>("foo"); 
        new Main(12); // use type inference 
    }
    
    function new(a:A) { } 
}