15. Haxeの言語構造 class型
class Foo {
// このクラスの外部からアクセスできる
public var a:Int;
// このクラスとそのサブクラスから
// アクセスできる (他言語のprotected相当)
private var b:String;
var c:Float; // private と同じ
static var d:Int; // 静的変数
}
16. Haxeの言語構造 class型
class Foo {
function foo():Void { }
// 引数と任意引数
function faa(a:Int, ?b:Int):Int {
if (b == null) {
return a;
} else {
return a + b;
}
}
}
17. Haxeの言語構造 class型
class Foo {
// 静的メソッド
static function bar() { }
// コンストラクタ
public function new() { }
// クラスイニシャライザ
static function __init__() { }
}
18. Haxeの言語構造 class型
class Foo {
public var a(get, set):Int;
public var b(get, null):Int;
var c(get, never):Int;
function get_a():Int { ... }
function set_a(v:Int) { ... }
function get_b():Int { ... }
function get_c():Int { ... }
}
30. Haxeの言語構造 abstract
型の拡張
@:coreType abstract ErrorCode from Int {}
@:coreType abstract ErrorCodeEx from Int
to Int {}
...
var a:ErrorCode = 1; // OK
var b:Int = a; // コンパイルエラー
var c:ErrorCodeEx;
var d:Int = c; // OK (to castが宣言されてい
る)
a = c; // OK
c = a; // NG
31. Haxeの言語構造 abstract
Opaque types
abstract StringExtension(String)
from String to String {
public function pluralize() {
return this + "s";
}
}
...
var s:StringExtension = "language";
trace(s.pluralize()); // "languages"
32. Haxeの言語構造 abstract
演算子のオーバーロード
typedef Point = { x:Float, y:Float };
abstract Vector(Point) from Point to Point {
@:op(A + B)
public static function add(lhs:Vector, rhs:Point): Vector {
var _lhs:Point = lhs;
return { x:_lhs.x + rhs.x, y: _lhs.y + rhs.y };
}
@:op(A * B)
public static function product(lhs:Vector, rhs:Point):
Float {
var _lhs:Point = lhs;
return _lhs.x * rhs.x + _lhs.y * rhs.y;
}
}
35. Haxeの言語構造 型パラメータ
class Base {}
class Extended extends Base {}
class GenericStuff<T:Base> {
public function new() {}
}
...
// コンパイルエラー (covariance)
var x:GenericStuff<Base> =
new GenericStuff<Extended>();
36. // ターゲット言語によってはコンパイルエラー
var x:Int = null;
// OK
var y:Null<Int> = null;
Haxeの言語構造 nullable
値型 (基本型) にはnullが代入できない
nullを代入可能にするには Null<> を用いる