简体   繁体   English

在编译为JavaScript时,有没有办法在Haxe中对别名进行别名?

[英]Is there a way to alias types in Haxe when compiling to JavaScript?

I have the following two classes: 我有以下两个类:

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 ...
}

I did this so I could overload operators in Vector2 . 我这样做了所以我可以在Vector2重载运算符。 This all works fine from inside of Haxe, however, we compile from Haxe to JavaScript. 这一切都在Haxe内部工作正常,但是,我们从Haxe编译为JavaScript。 Our output JavaScript is then used in mocha tests. 然后我们的输出JavaScript用于mocha测试。

Unfortunately, classes declared abstract are completely omitted from the JS code (well, this isn't entirely true, but they do look something like: 不幸的是,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);
};

So, in my tests, instead of using Vector2 , I am forced to use Vector2Impl . 所以,在我的测试,而不是使用的Vector2 ,我被迫使用Vector2Impl This is a bit of a pain, because a) it requires more typing, and b) it exposes an inconsistent interface between Haxe clients and JavaScript clients. 这有点痛苦,因为a)它需要更多的输入,并且b)它暴露了Haxe客户端和JavaScript客户端之间不一致的接口。 I could deal with it, but since Haxe isn't completely compiling out the type Vector2 , I'm wondering if there is a way I can alias Vector2Impl to Vector2 inside of the generated .js file. 我可以对付它,但由于HAXE没有完全编译出来的类型Vector2 ,我不知道是否有一种方法可以让我别名Vector2ImplVector2所产生的内部.js文件。 That way, instead of doing the following in my tests: 这样,而不是在我的测试中执行以下操作:

'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);
    });
  });
});

I could do this: 我能做到这一点:

'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);
    });
  });
});

Obviously, I know that I can't use the operator overloads in JavaScript, since the language doesn't support it, but if I could use all the other methods available in Vector2Impl by calling it Vector2 , that would be awesome. 很显然,我知道,我不能在JavaScript中使用操作符重载,因为语言不支持它,但如果我可以使用所有可用其他方法Vector2Impl通过调用它Vector2 ,这将是真棒。

NOTE : I know I could just do this: 注意 :我知道我可以这样做:

let Vector2 = geometer.Vector2Impl;

But, I'd rather the client not even be aware of the Vector2Impl class, if possible. 但是,如果可能的话,我宁愿客户端甚至不知道Vector2Impl类。

You could simply annotate Vector2DImpl with @:expose metadata : 你可以简单地用@:expose注释Vector2DImpl @:expose元数据

@:expose("geometer.Vector2D")
class Vector2Impl {
    ...
}

This includes it in $hx_exports as geometer.Vector2D . 这包括它$hx_exportsgeometer.Vector2D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM