-
-
Notifications
You must be signed in to change notification settings - Fork 662
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Calling Context.defineModule in Context.onTypeNotFound still errors #4234
Comments
This issue may duplicate with #3262 |
Sorry, for the noise. The discussion in the other issue at some point gets very close to this, but it seems I am actually after something slightly different after all. |
I don"t know why all issues about Context.defineModule confuse me so much that I never quite understand what exactly you guys want. It has arguments for import and using now, what else is missing? |
Hope this clarifies: there is no good way to combine the defineModule API (which does everything I want it to) and onTypeNotFound. I would like this to work: package;
class Test {
#if macro
static function use() {
haxe.macro.Context.onTypeNotFound(function (name:String) {
if (name == "Foo") {
var type = macro class Foo { };
haxe.macro.Context.defineModule("Foo", [type]);
//return type;
}
return null;
});
}
#else
static function main() {
Foo;//yields "Unknown identifier : Foo
" or when the above return is uncommented, yields "Type name Foo is redefined from module Foo"
}
#end
} As the code above shows, the module does get defined, but returning Short of adding an |
But what"s wrong with |
As I said, that"s interpreted as a duplicate definition. I already define the type with |
Ah, now I get it. |
I wish we could change the callback type of onTypeNotFound to |
Hm, how about returning a typedef to the type in the module? Or could we add a special typedef or abstract that"s parameterized with the desired type within the module? It doesn"t feel right to have the compiler do the lookup a second time for every null from onTypeNotFound. |
@ousado I was hoping for the typedef to work, but that yields a duplicate type error. But it would do the trick. The special type doesn"t feel right either ;) @Simn Why not abstract TypeNotFoundCallback(String->Null<Type>) from String->Null<Type> {
@:from static function fromLegacyVersion(f:String->Null<TypeDefinition>):TypeNotFoundCallback
return function (name) return switch f(name) {
case null: null;
case v:
//the compiler currently just ignores the name and some code may rely on that
var parts = name.split(".");
v.name = parts.pop();
v.pack = parts;
Context.defineType(v);
Context.getType(name);
}
} It might bite a few people who depended on inference, but I"d say that"s fair enough. Happens to me all the time :D |
There were reasons for changing |
Alright, forget that bit. What about building it over |
I"m curious, does |
Are there any news on this? I don"t want to be impatient, but if this is easy to add, then I"d appreciate it a lot. I have found my own workarounds, but they make any reported errors less than useless. |
Well, the last comment was me asking something so I guess I was waiting for a reply. I still think changing the return type to |
Well, there"s 10 occurrences of But if you do feel differently, why not have the compiler side expect one and make the API accept both? static function onTypeNotFound(cb:EitherType< String->Null<ComplexType> , String->Null<TypeDefinition>>) {
function find(name:String):Null<ComplexType> {
var ret:Dynamic = cb(name);
return
if (ret == null || Std.is(ret, ComplexType)) ret;
else {
var td:TypeDefinition = ret;
Context.defineType(td);
TPath( /* ... the path either from the td or from the name ?... */ );
}
}
load("on_type_not_found",1)(find);
} |
I propose adding a new function: public static function onModuleNotFound(cb:String->Array<TypeDefinition>); So we can return all types in that module. |
I"ve looked at this for a bit to understand the problem. The What you want here is a bit different: It"s essentially a path forwarding/rewriting, with the option of using the input path again after defining a type. This means we have to handle it differently internally. This isn"t hard to do, the only question is if This would allow infinite recursion if you return the input path without actually defining the type. That may or may not be the user"s fault though... |
How about returning |
If we really want to avoid breaking things, I would propose to use |
Slightly related: Context.defineType could return a ComplexType which identifies the defined type. |
EitherType is a hack to deal with hacks. It shouldn"t be used in any API one has control over, let alone in a core compiler API. |
Coming back to this, I realize that using I think what we want here is a simple dot path, aka a String. |
Because TypeDefinition lacks a few features (mostly import and using), it would be nicer to be able to call defineModule and have the compiler check again whether the type is still undefined after invoking all callbacks. I know that"s not exactly pretty, but at least it wouldn"t break any APIs.
The text was updated successfully, but these errors were encountered: