[英]Is there a way to alias types in Haxe when compiling to JavaScript?
我有以下兩個類:
package geometer;
class Vector2Impl {
public var x: Float;
public var y: Float;
... operations and methods...
package geometer;
@:forward(x, y)
abstract Vector2(Vector2Impl) from Vector2Impl to Vector2Impl {
public
function new(x: Float, y: Float) {
this = new Vector2Impl(x, y);
}
... operator overloads ...
}
我這樣做了所以我可以在Vector2
重載運算符。 這一切都在Haxe內部工作正常,但是,我們從Haxe編譯為JavaScript。 然后我們的輸出JavaScript用於mocha測試。
不幸的是,JS代碼中完全省略了聲明為abstract
類(嗯,這不完全正確,但它們看起來像:
var geometer__$Vector2_Vector2_$Impl_$ = {};
geometer__$Vector2_Vector2_$Impl_$.__name__ = true;
geometer__$Vector2_Vector2_$Impl_$._new = function(x,y) {
return new geometer_Vector2Impl(x,y);
};
所以,在我的測試,而不是使用的Vector2
,我被迫使用Vector2Impl
。 這有點痛苦,因為a)它需要更多的輸入,並且b)它暴露了Haxe客戶端和JavaScript客戶端之間不一致的接口。 我可以對付它,但由於HAXE沒有完全編譯出來的類型Vector2
,我不知道是否有一種方法可以讓我別名Vector2Impl
到Vector2
所產生的內部.js
文件。 這樣,而不是在我的測試中執行以下操作:
'use strict';
let expect = require('chai').expect;
let library = require('../../dist/library.js');
let geometer = library.geometer;
// Unfortunately, abstract classes (Vector2) are not available in JS-land.
let Vector2Impl = geometer.Vector2Impl;
require('../nyc-reporter');
describe('Vector2Impl', function() {
describe ('#new', function() {
it ('should create a new Vector2Impl object', function() {
let v = new Vector2Impl(20.5, 100.0);
expect(v.x).to.eq(20.5);
expect(v.y).to.eq(100.0);
});
});
});
我能做到這一點:
'use strict';
let expect = require('chai').expect;
let library = require('../../dist/library.js');
let geometer = library.geometer;
// Unfortunately, abstract classes (Vector2) are not available in JS-land.
let Vector2 = geometer.Vector2;
require('../nyc-reporter');
describe('Vector2', function() {
describe ('#new', function() {
it ('should create a new Vector2 object', function() {
let v = new Vector2(20.5, 100.0);
expect(v.x).to.eq(20.5);
expect(v.y).to.eq(100.0);
});
});
});
很顯然,我知道,我不能在JavaScript中使用操作符重載,因為語言不支持它,但如果我可以使用所有可用其他方法Vector2Impl
通過調用它Vector2
,這將是真棒。
注意 :我知道我可以這樣做:
let Vector2 = geometer.Vector2Impl;
但是,如果可能的話,我寧願客戶端甚至不知道Vector2Impl
類。
你可以簡單地用@:expose
注釋Vector2DImpl
@:expose
元數據 :
@:expose("geometer.Vector2D")
class Vector2Impl {
...
}
這包括它$hx_exports
為geometer.Vector2D
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.