haxetink / tink_macro

The macro toolkit
MIT License
57 stars 17 forks source link

[MacroApi.hx] Class not found : tink.CoreApi; #2

Closed chamberlainpi closed 9 years ago

chamberlainpi commented 9 years ago

When I tried compiling this in a FlashDevelop project, got this error: [HaxeLibraries]/Tink_Macros/src/tink/MacroApi.hx:5: characters 0-19 : Class not found : tink.CoreApi

Error preview of 'using tink.CoreApi'

Commenting it out doesn't resolve the issue (generates more problems). Is this class missing from the repo? (maybe I didn't clone it right :/)

back2dos commented 9 years ago

As said in the readme, tink_macro is built on tink_core, hence you will need to get that as well ;) Personally, I would suggest getting the library from haxelib, as that will deal with dependencies for you.

chamberlainpi commented 9 years ago

Ah whoops, overlooked that part. Thanks, will try getting it via haxelib (under the project name "tink_core" I take it? or all one word?)

EDIT: nvm, got it! (haxelib install tink_core worked)

chamberlainpi commented 9 years ago

Hmm, the issue isn't quite resolved yet even after including -lib tink_core (after installing it).

The error I'm getting now is this: MacroApi.hx:38: characters 9-38 : Class<haxe.macro.Context> has no field currentPos

There's a number of other issues to do with Context has no field ..., maybe I'm missing some import/using higher up where I'm using tink_core? MacroApi.hx:38: characters 9-38 : Class<haxe.macro.Context> has no field currentPos

EDIT: Here's a longer list of the errors reported in FlashDevelop: FD errors with tink_core

back2dos commented 9 years ago

Thanks, will try getting it via haxelib (under the project name "tink_core" I take it? or all one word?)

If you haxelib install tink_macro then tink_core will be installed also.

There's a number of other issues to do with Context has no field ..., maybe I'm missing some import/using higher up where I'm using tink_core?

This is indicative of your macros being compiled into your runtime code, which they shouldn't be. Try to make sure that all code that is not in itself a macro but is only used at macro time is wrapped in a #if macro directive.

Here's an example:

//File: Main.hx
package ;

import test.MyMacro;

class Main {
   static function main() {
      MyMacro.call();
   }
}

//File: test/MyMacro.hx

package ;

#if macro
import haxe.macro.Context;
import haxe.macro.Expr;
#end

class MyMacro {
  macro static public function call() {
    explain();
    return macro trace("hello world");
  }
  #if macro
  static function explain() {
    Context.warning('This will become `trace("hello world")`', Context.currentPos());
  }
  #end
}

If you omit the #if macro then the compiler will see Main depending on test.MyMacro which in turn interacts with haxe.macro.Context, the runtime version of which has almost no methods (obviously, since the macro context does not exist anymore).

It is important to understand that every class can have a macro and a runtime version. This is something to just get an intuition for. It's not mindbogglingly hard, but crucial to understand. And while I am very happy to see new users of this library, I would still suggest you make your first macro experiments without it.

This library doesn't make Haxe macro programming conceptually easier like maybe jQuery does with JavaScript, but rather is a collection of tools for common problems coming up every now and then, similarly to what the as3corelib does in an ActionScript context. The analogy is very crude, but I hope you catch my drift ;)

chamberlainpi commented 9 years ago

Yeah the analogy makes sense. I'll definitely need more practice writing pure macros before I randomly start inserting tink_macro calls everywhere. I appreciate your help and that example will come in handy! :)