python - 我怎样才能连同我的类公开从 glm Haxe 库导入的类型?

标签 python haxe haxelib

我正在编写一个类,我将把它翻译成 Python 和 C#。 我的代码使用了很好的“glm”库。 Glm 提供有用的数据类型,例如:Vec3

我可以让 Vec3 对我类(class)的 Python 和 C# 用户可见吗? 换句话说,我可以使用 Vec3 数据类型公开公共(public)方法吗?

这是一个示例 Haxe 代码,其中包含一个公共(public)函数使用 Vec3 类型的类:

// Test execution:
// haxe -main TestGLM1 -lib glm --interp
// Translate into Python:
// haxe -main TestGLM1 -python TestGLM1.py  -lib glm 
// python3 TestGLM1.py

import glm.Vec3;
import glm.Quat;
import glm.Mat4;


class TestGLM1 {

    public function new() {
    }

    static function main() {

        // My test of the method taking 3D datatypes as input and output
        var t: TestGLM1 = new TestGLM1() ;
        var v1: Vec3 = new Vec3(1,2,3) ;
        var v2: Vec3 = new Vec3(7,8,9) ;
        t.testVecIO(v1, v2);

        trace(v1, v2);
    }

    public function testVecIO(vec_in: Vec3 , vec_out: Vec3) {
        vec_out.x = vec_in.x + vec_out.x;
        vec_out.y = vec_in.y + vec_out.y;
        vec_out.z = vec_in.z + vec_out.z;
    }
}

我想像这样编写一个 Python 测试:

from TestGLM1 import TestGLM1
from TestGLM1 import glm_Vec3

print("Python Test...")

t = TestGLM1()

v1 = glm_Vec3(1,2,3)  # Vec3()
v2 = glm_Vec3(4,5,6)  # Vec3()
t.testVecIO(v1, v2)
print(v1, v2)

print("Python done.")

但是,这个测试失败了:

ImportError: cannot import name 'glm_Vec3'

因为我在 TestGLM1.py 中可以看到的唯一类是:

class glm_Vec3Base:
    _hx_class_name = "glm.Vec3Base"
    __slots__ = ("x", "y", "z")
    _hx_fields = ["x", "y", "z"]

    def __init__(self):
        self.z = None
        self.y = None
        self.x = None

它既有不友好的名称,也没有显示正确的构造函数。

有什么建议吗? 谢谢。

最佳答案

the only class that I can see in TestGLM1.py is glm_Vec3Base

那是因为 Vec3 是一个 abstract type ,而摘要仅存在于编译时。底层类型确实是一个名为 Vec3Base 的类,如果您检查 Vec3.hx,您会注意到为什么它“没有合适的构造函数”:

class Vec3Base {
    function new() {}

    var x:Float;
    var y:Float;
    var z:Float;
}

您还可以注意到 Vec3 的所有方法都声明为 inline。如果它们不是(您可以通过使用 --no-inline 进行编译来强制这样做),您的 Python 输出将有一个名为 glm__Vec3_Vec3_Impl_ 的附加类。它具有用于所有 Vec3 方法的静态方法,因为这就是抽象在幕后的工作方式:

A good question at this point is "What happens if a member function is not declared inline" because the code obviously has to go somewhere. Haxe creates a private class, known to be the implementation class, which has all the abstract member functions as static functions accepting an additional first argument this of the underlying type.

总而言之,由于 Vec3 是作为抽象实现的,我认为没有方便的方法将它公开给 Python 或 C# 用户。通常,Haxe 生成的代码不太适合这样使用。这并不是说根本没有促进它的机制。例如,有 @:expose JavaScript 和 Lua 目标的元数据。

关于python - 我怎样才能连同我的类公开从 glm Haxe 库导入的类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48669998/

相关文章:

python - Pandas 索引和 key 错误

python - 在文本文件中打印字符百分比

class - 获取 Haxe 中具有空值的类字段的类型

python - 两个标签之间的 re.sub

python - Pandas 注释数据框历史

rest - 如何从 Haxe/Neko 发送 HTTP PUT 请求?

ruby - 使用哪种群发消息技术?

ssl - Neko hxssl 不适用于 HTTPS

haxe - 在Haxe中执行外部程序中的URL/路径

segmentation-fault - 由于段错误,OpenFL 和 Lime 无法运行