3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Haxeと構造的部分型付け

Last updated at Posted at 2013-05-03

Haxeがサポートしている「構造的部分型付け(Structural Subtyping)」という素晴らしい機能について紹介します。

まずは、サンプルコードを見てください。

Main.hx
class Main {
	static function main() {
		start( new Human() );	//エラーなし
		start( new Car() );		//エラーなし
		start( new Fish() );	//コンパイルエラー!!
	}
	
	static public function start( runner ) {
		runner.run();	//変数runnerには、run関数が必要。
	}
}

class Human {
	public function new(){}
	public function run() {
		trace( "人は走るよ!" );
	}
}

class Car {
	public function new(){}
	public function run() {
		trace( "車も走るよ!" );
	}
}

class Fish {
	public function new(){}
}

上の例では、start()にHuman、Car、Fishを渡していますが、Fishでのみコンパイルエラーが発生してます。これは、runnerが「run関数を持つ型」として型推論されており、そこにrun関数をもたないFishを渡しために起きたエラーです。

このように、クラスがどのような変数や関数を持ってるかという構造に対して、部分的に型付けできるのが、「構造的部分型付け」です。


さらに、「run関数を持つ型」というのは明示的に宣言することが可能です。
つまり、サンプルコードのstart()は以下のように書きかえることができます。

start()
	static public function start( runner: { function run():Void; } ) {
		runner.run();
	}

さらに詳しい話については、公式ドキュメントの構造体を見てください

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?