# Wildcards and resolving classes

**URL:** https://community.haxe.org/t/wildcards-and-resolving-classes/1993
**Category:** Haxe
**Created:** [August 29, 2019, 10:25am UTC](https://community.haxe.org/t/wildcards-and-resolving-classes/1993 "2019-08-29T10:25:34Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![GiG](https://community.haxe.org/user_avatar/community.haxe.org/gig/32/995_2.png) [@GiG](https://community.haxe.org/u/GiG)
#### Post date: [August 29, 2019, 10:25am UTC](https://community.haxe.org/t/wildcards-and-resolving-classes/1993/1 "2019-08-29T10:25:34Z")

</div>

Hi guys!  
I am new here, even if I am in the Openfl forum since… well… maybe 2015.  
Who cares, let’s go!

Maybe I know the answer, but I also know that I don’t know everything so I am asking you a probably-noob question: can I resolve a class with

`Type.resolveClass`

if the class has been “imported” with a wildcard?  
An example, I need to resolve class

`custom.gameName.symbols.SymbolA`

and I imported the whole folder content by

`import custom.gameName.symbols.*`

because the folder contains tons of classes.

`Type.resolveClass("custom.gameName.symbols.SymbolA")`

doesn’t work, and this is why I am here, but maybe you can give me an advice to bypass/fix the problem.

Thanks!

---

<div class="post-metadata">

### Author: ![aliokan](https://community.haxe.org/user_avatar/community.haxe.org/aliokan/32/182_2.png) [@aliokan](https://community.haxe.org/u/aliokan)
#### Post date: [August 29, 2019, 11:09am UTC](https://community.haxe.org/t/wildcards-and-resolving-classes/1993/2 "2019-08-29T11:09:02Z")

</div>

The dead code elimination removes the class import if it doesn’t find the class use in the source code. It’s the case with ‘Type.resolveClass’ (the resolution is made at runtime and not during the compilation).

As you can see in the documentation, you can force keeping the class with: add ‘@keep’ in you class, or use ‘–macro keep()’ in the hxml, or completely disable the DCE with -dce no

> **[Dead Code Elimination - Haxe - The Cross-platform Toolkit](https://haxe.org/manual/cr-dce.html)**
>
> Haxe is an open source toolkit based on a modern, high level, strictly typed programming language.

---

<div class="post-metadata">

### Author: ![GiG](https://community.haxe.org/user_avatar/community.haxe.org/gig/32/995_2.png) [@GiG](https://community.haxe.org/u/GiG)
#### Post date: [August 29, 2019, 1:17pm UTC](https://community.haxe.org/t/wildcards-and-resolving-classes/1993/3 "2019-08-29T13:17:52Z")

</div>

Ok, now I understand what DCE is doing here.

I am adding more details about my case:  
I am using conditional compilation

```haxe
#if (gameName == "A")
import custom.gameName.symbols.SymbolA;
import custom.gameName.symbols.SymbolB;

#elseif (gameName == "B")

import custom.gameName.symbols.SymbolA;
import custom.gameName.symbols.SymbolB;
import custom.gameName.symbols.SymbolC;
import custom.gameName.symbols.SymbolD;
...
import custom.gameName.symbols.SymbolX;
import custom.gameName.symbols.SymbolZ;

#elseif (gameName == "C")
...

```

Replacing those lists with  
`import custom.gameName.symbols.*;`  
would be great.

Where should I put @:keep to keep the imports with the wildcard despite DCE?

---

<div class="post-metadata">

### Author: ![Gama11](https://community.haxe.org/user_avatar/community.haxe.org/gama11/32/7_2.png) [@Gama11](https://community.haxe.org/u/Gama11)
#### Post date: [August 29, 2019, 2:21pm UTC](https://community.haxe.org/t/wildcards-and-resolving-classes/1993/4 "2019-08-29T14:21:29Z")

</div>

That’s unrelated to DCE. See [Haxe: how to keep unused class from being eliminated - Stack Overflow](https://stackoverflow.com/questions/42021607/haxe-how-to-keep-unused-class-from-being-eliminated/).

---

<div class="post-metadata">

### Author: ![aliokan](https://community.haxe.org/user_avatar/community.haxe.org/aliokan/32/182_2.png) [@aliokan](https://community.haxe.org/u/aliokan)
#### Post date: [August 29, 2019, 2:27pm UTC](https://community.haxe.org/t/wildcards-and-resolving-classes/1993/5 "2019-08-29T14:27:37Z")

</div>

@:keep should be added before your Symbol class definition. Like:

```haxe
@:keep
class SymbolX {}

```

In case of all your symbole extends the same parent classes, you can use @:keepSub, and all the subclass will be protected against DCE.

---

<div class="post-metadata">

### Author: ![aliokan](https://community.haxe.org/user_avatar/community.haxe.org/aliokan/32/182_2.png) [@aliokan](https://community.haxe.org/u/aliokan)
#### Post date: [August 29, 2019, 2:41pm UTC](https://community.haxe.org/t/wildcards-and-resolving-classes/1993/6 "2019-08-29T14:41:37Z")

</div>

Effectively, --macro include() it’s a good option, but import classes and @:keep it’s also an option.

---

<div class="post-metadata">

### Author: ![GiG](https://community.haxe.org/user_avatar/community.haxe.org/gig/32/995_2.png) [@GiG](https://community.haxe.org/u/GiG)
#### Post date: [August 29, 2019, 3:34pm UTC](https://community.haxe.org/t/wildcards-and-resolving-classes/1993/7 "2019-08-29T15:34:21Z")

</div>

I already tried to add the @:keep in all the game-related -SymbolX- classes and import them with the wildcarded import, but the game couldn’t resolve the Class at runtime.

It looks like I still have to list all the imports and that the wildcard is not an option, right?

---

<div class="post-metadata">

### Author: ![Gama11](https://community.haxe.org/user_avatar/community.haxe.org/gama11/32/7_2.png) [@Gama11](https://community.haxe.org/u/Gama11)
#### Post date: [August 29, 2019, 5:50pm UTC](https://community.haxe.org/t/wildcards-and-resolving-classes/1993/8 "2019-08-29T17:50:46Z")

</div>

The StackOverflow answer I linked explains this. @:keep doesn’t matter if the compiler never gets to see the type, which it doesn’t with wildcard imports because they’re lazy.

---

<div class="post-metadata">

### Author: ![GiG](https://community.haxe.org/user_avatar/community.haxe.org/gig/32/995_2.png) [@GiG](https://community.haxe.org/u/GiG)
#### Post date: [August 30, 2019, 9:47am UTC](https://community.haxe.org/t/wildcards-and-resolving-classes/1993/9 "2019-08-30T09:47:58Z")

</div>

Ok, so if I put @:keep in the SymbolX classes I should be able to reach them even if the wildcarded import is removed by DCE, right? But it doesn’t work, even if I put both @:keep in the classes and @:keepSub in the super, and disable dce.  
This because even if the compiler keeps the classes, without the explicit import it doesn’t know where the classes are located, as they are in a different folder, is this correct?

I would need an active (non-lazy) wildcard, but such an option would be very dangerous in the wrong hands.

---

<div class="post-metadata">

### Author: ![Gama11](https://community.haxe.org/user_avatar/community.haxe.org/gama11/32/7_2.png) [@Gama11](https://community.haxe.org/u/Gama11)
#### Post date: [August 30, 2019, 10:00am UTC](https://community.haxe.org/t/wildcards-and-resolving-classes/1993/10 "2019-08-30T10:00:02Z")

</div>

> I should be able to reach them even if the wildcarded import is removed by DCE

Again, this has nothing to do with DCE.

> I would need an active (non-lazy) wildcard

`--macro include()` (again, as mentioned in my SO answer) is essentially an eager wildcard import.

---

<div class="post-metadata">

### Author: ![GiG](https://community.haxe.org/user_avatar/community.haxe.org/gig/32/995_2.png) [@GiG](https://community.haxe.org/u/GiG)
#### Post date: [August 30, 2019, 10:26am UTC](https://community.haxe.org/t/wildcards-and-resolving-classes/1993/11 "2019-08-30T10:26:18Z")

</div>

Sorry, I should read more carefully.  
So I have to include the package:  
I can’t do it in the command line, how can I add (include) it in the block

```haxe
#if (gameName == "A")
...
#end

```

with a macro?  
I am pretty new to macros and in general this kind of operations 😅

---

<div class="post-metadata">

### Author: ![Gama11](https://community.haxe.org/user_avatar/community.haxe.org/gama11/32/7_2.png) [@Gama11](https://community.haxe.org/u/Gama11)
#### Post date: [August 30, 2019, 10:43am UTC](https://community.haxe.org/t/wildcards-and-resolving-classes/1993/12 "2019-08-30T10:43:05Z")

</div>

You can make an initialization macro of your own that calls `include()` (which is also an init macro).

```haxe
--macro Macro.init()

```

```haxe
class Macro {
	static function init() {
		#if (gameName == "A")
		haxe.macro.Compiler.include("package");
		#end
	}
}

```

> **[Initialization Macros - Haxe - The Cross-platform Toolkit](https://haxe.org/manual/macro-initialization.html)**
>
> Haxe is an open source toolkit based on a modern, high level, strictly typed programming language.

---

<div class="post-metadata">

### Author: ![GiG](https://community.haxe.org/user_avatar/community.haxe.org/gig/32/995_2.png) [@GiG](https://community.haxe.org/u/GiG)
#### Post date: [August 30, 2019, 11:12am UTC](https://community.haxe.org/t/wildcards-and-resolving-classes/1993/13 "2019-08-30T11:12:54Z")

</div>

I have added the function

```haxe
public static function init() {
	#if (gameName == "A")
haxe.macro.Compiler.include("***.symbols");
	#end
}

```

in the class MyClass where all the imports are made, but I don’t know how to add

`--macro MyClass.init()`

because the panel Compiler Options → Additional Options in HaxeDevelop does not allow to add anything, and if I add it directly in the .hxproj it ignores and removes it

**EDIT** : ok, maybe I have found the right way to add the option in the hxproj, but now I am probably setting the wrong relative path… 😐

---

<div class="post-metadata">

### Author: ![Gama11](https://community.haxe.org/user_avatar/community.haxe.org/gama11/32/7_2.png) [@Gama11](https://community.haxe.org/u/Gama11)
#### Post date: [August 30, 2019, 11:58am UTC](https://community.haxe.org/t/wildcards-and-resolving-classes/1993/14 "2019-08-30T11:58:13Z")

</div>

You only have a .hxproj, no HXML file? Personally I would avoid that, makes you very IDE-dependent and you can’t even easily build from the command line. FD can work with HXML files too.

---

<div class="post-metadata">

### Author: ![GiG](https://community.haxe.org/user_avatar/community.haxe.org/gig/32/995_2.png) [@GiG](https://community.haxe.org/u/GiG)
#### Post date: [August 30, 2019, 12:40pm UTC](https://community.haxe.org/t/wildcards-and-resolving-classes/1993/15 "2019-08-30T12:40:18Z")

</div>

Well, in the bin/html5/haxe folder there are debug.hxml and release.hxml, I suppose they are imported by the template project, so the base versions must be somewh… here they are

```haxe
HaxeToolkit\haxe\lib\openfl\8,9,2\assets\templates\html5\hxml\debug.hxml
HaxeToolkit\haxe\lib\openfl\8,9,2\assets\templates\html5\hxml\release.hxml

```

Do I have to inject the instruction to run .init() in here?

---

<div class="post-metadata">

### Author: ![Gama11](https://community.haxe.org/user_avatar/community.haxe.org/gama11/32/7_2.png) [@Gama11](https://community.haxe.org/u/Gama11)
#### Post date: [August 30, 2019, 12:50pm UTC](https://community.haxe.org/t/wildcards-and-resolving-classes/1993/16 "2019-08-30T12:50:58Z")

</div>

Oh, if it’s an OpenFL project, the .hxproj file is pretty irrelevant. In that case, all the build arguments are in your project.xml. Something like this should work:

```haxe
<haxeflag name="--macro" value="Macro.init()" />

```

You should not be editing the hxml files generated by Lime, they will just be overwritten during the next build.

---

<div class="post-metadata">

### Author: ![GiG](https://community.haxe.org/user_avatar/community.haxe.org/gig/32/995_2.png) [@GiG](https://community.haxe.org/u/GiG)
#### Post date: [August 30, 2019, 1:06pm UTC](https://community.haxe.org/t/wildcards-and-resolving-classes/1993/17 "2019-08-30T13:06:42Z")

</div>

Oh, yes, I mean to inject in the template.  
Well, if I can do it in the project.xml it is perfect, but I have the same problem:  
how do I specify the path to MyClass.init()?  
The source path

`<source path="..\..\..\..\Library\Base" />`

is the root, but I have to reach MyClass, that is in a subfolder  
If I put Macro.hx in the root it works, but I need to add the includes in the specific class to centralize everything.  
This really looks like a noob problem… I’m feeling sad 😑

---

<div class="post-metadata">

### Author: ![Gama11](https://community.haxe.org/user_avatar/community.haxe.org/gama11/32/7_2.png) [@Gama11](https://community.haxe.org/u/Gama11)
#### Post date: [August 30, 2019, 1:19pm UTC](https://community.haxe.org/t/wildcards-and-resolving-classes/1993/18 "2019-08-30T13:19:01Z")

</div>

The path isn’t relevant here, the _package_ is, just like when you import a type. If there’s `package my.pack;` in `Macro.hx`, it’d be `my.pack.Macro.init()`.

---

<div class="post-metadata">

### Author: ![GiG](https://community.haxe.org/user_avatar/community.haxe.org/gig/32/995_2.png) [@GiG](https://community.haxe.org/u/GiG)
#### Post date: [August 30, 2019, 1:26pm UTC](https://community.haxe.org/t/wildcards-and-resolving-classes/1993/19 "2019-08-30T13:26:37Z")

</div>

I know, it is exactly what I did, but I get these errors

 ![image](https://community.haxe.org/uploads/default/original/2X/8/82d941c72c57957cd047d8e9d5155f7a620f6ec2.png)

😅

The init function is identical, I just added the package path before MyClass.init() in project.xml

---

<div class="post-metadata">

### Author: ![Gama11](https://community.haxe.org/user_avatar/community.haxe.org/gama11/32/7_2.png) [@Gama11](https://community.haxe.org/u/Gama11)
#### Post date: [August 30, 2019, 1:38pm UTC](https://community.haxe.org/t/wildcards-and-resolving-classes/1993/20 "2019-08-30T13:38:11Z")

</div>

Hm, maybe something else comes across it for some reason… does it help to surround the module in `#if macro`?

[Next page](https://community.haxe.org/t/wildcards-and-resolving-classes/1993.md?page=2)
