我如何在不先导入类型的情况下解析类? [英] How do I resolveClass without importing the type first?

查看:73
本文介绍了我如何在不先导入类型的情况下解析类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意,我在针对Neko的Linux上使用Haxe 3.0.1.

考虑以下代码:

var classObject = Type.resolveClass("Foo");

假设我在与此代码相同的程序包中有一个类Foo,这应该给我一个Class对象,其中包含有关Foo的信息.但是,在我的测试中,仅当我先import Foo;时它才起作用.如果我不导入Foo,则resolveClass会返回null.这对我来说很奇怪.

Assuming that I have a class Foo in the same package as this code, this should give me a Class object with info about Foo in it. However, in my tests it is only working if I import Foo; first. If I do not import Foo then resolveClass returns null instead. This is strange to me.

我注意到 Haxe API 文档指出:

该类必须已被编译为可访问的,并且如果该类不在同一包中,则完整的点路径"将被编译.必须作为字符串的一部分给出.

The class must have been compiled to be accessible and if the Class is not in the same package, the full "dot path" must be given as part of the string.

但是对于我来说,尚不清楚什么必须已被编译为可访问的".是指在这种情况下或如何将其应用于我的问题.

But it is not clear to me what "must have been compiled to be accessible" means in this context or how it applies to my problem.

那么,我真的需要import我想解决的每个类吗,还是我错过了其他事情?

So, do I really need to import every class that I might want to resolve, or am I missing something else?

推荐答案

首先,应为Type.resolveClass提供类的完全限定名称. IE. pack.subpack.AClass.

Firstly, you should give Type.resolveClass the fully qualified name of the class. I.e. pack.subpack.AClass.

第二,您可以使用--macro include("pack"). include函数是 haxe.macro.Compiler 中的方法. --macro默认情况下会在该类中查找方法.

Secondly, you may use --macro include("pack"). Which the include function is a method from haxe.macro.Compiler. --macro by default looks into that class for methods.

最后,haxe中的import my.package.*语句是惰性的.它告诉编译器解析my.package中的类型,但一开始并没有真正将所有内容导入my.package中.

Lastly, the import my.package.* statement in haxe is lazy. It tell the compiler to resolve types in my.package, but not really import everything in my.package in the beginning.

这篇关于我如何在不先导入类型的情况下解析类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆