Java 列挙型に基づいたライブラリを移植していて、それらがネイティブにサポートされるまで独自の列挙型をコーディングする必要があります。
しかし、私は失敗します!
以下のコードでは、ChessColor.values() メソッドが null を返しますが、その理由がわかりません。
しかし、私はDartに慣れていません...
私が見逃した静的フィールドと初期化に何かがあるに違いありません...
列挙型基本クラス
part of chessmodel;
/**
* Emulation of Java Enum class.
*/
abstract class Enum {
final int code;
final String name;
Enum(this.code, this.name);
toString() => name;
}
簡単な使い方のお試し
part of chessmodel;
final ChessColor WHITE = ChessColor.WHITE;
final ChessColor BLACK = ChessColor.BLACK;
class ChessColor extends Enum {
static List<ChessColor> _values;
static Map<String, ChessColor> _valueByName;
static ChessColor WHITE = new ChessColor._x(0, "WHITE");
static ChessColor BLACK = new ChessColor._x(1, "BLACK");
ChessColor._x(int code, String name) : super (code, name) {
if (_values == null) {
_values = new List<ChessColor>();
_valueByName = new Map<String, ChessColor>();
}
_values.add(this);
_valueByName[name] = this;
}
static List<ChessColor> values() {
return _values;
}
static ChessColor valueOf(String name) {
return _valueByName [name];
}
ChessColor opponent() {
return this == WHITE ? BLACK : WHITE;
}
bool isWhite() {
return this == WHITE;
}
bool isBlack() {
return this == BLACK;
}
}