Я пытаюсь создать haxe.ds.HashMap
, где ключи являются объектом, который я не контролирую. Таким образом, они не реализуют метод hashCode
, и я не могу их изменить.Использование тегов в качестве ключей HashMap в Haxe
Я бы очень хотел использовать реферат, чтобы выполнить это, но я получаю некоторые ошибки времени компиляции.
Here is the code Я играю с:
import haxe.ds.HashMap;
abstract IntArrayKey(Array<Int>) from Array<Int> {
inline public function new(i: Array<Int>) {
this = i;
}
public function hashCode(): Int {
// General warning: Don't copy the following line. Seriously don't.
return this.length;
}
}
class Test {
static function main() {
var hash = new HashMap<IntArrayKey, Bool>();
}
}
компиляция ошибки:
Test.hx:15: characters 19-51 : Constraint check failure for haxe.ds.HashMap.K
Test.hx:15: characters 19-51 : IntArrayKey should be { hashCode : Void -> Int }
Но в тот момент я change my abstract over to a class, он отлично компилируется:
import haxe.ds.HashMap;
class IntArrayKey {
private var _i: Array<Int>;
inline public function new(i: Array<Int>) {
this._i = i;
}
public function hashCode(): Int {
// General warning: Don't copy the following line. Seriously don't.
return this._i.length;
}
}
class Test {
static function main() {
var hash = new HashMap<IntArrayKey, Bool>();
}
}
Это точно тот же hashCode
реализация, просто другой контекст. Есть ли способ сделать это? Или это ограничение языка?
Подтверждено людьми Haxe, что это по дизайну: https://github.com/HaxeFoundation/haxe/issues/3708 – Nycto