Last active
July 28, 2017 09:02
-
-
Save back2dos/590eb021c9f1734bdcd1887a552e771d to your computer and use it in GitHub Desktop.
Reference local class as `Self`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ; | |
#if macro | |
import haxe.macro.Context.*; | |
class Self { | |
static public function buildSelf() | |
return getType( | |
switch getTypedExpr(typeExpr(macro (function (?pos:haxe.PosInfos) {})())).expr { | |
case ECall(_, [macro { fileName: $_, lineNumber: $_, className: ${{ expr: EConst(CString(name)) }}, methodName: $_ }]): name; | |
default: throw 'assert'; | |
} | |
); | |
} | |
#else | |
@:genericBuild(Self.buildSelf()) | |
class Self {} | |
#end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage example: http://try-haxe.mrcdk.com/#72855
Please note that this does not work across inheritance.
Meaning:
All this type does is to refer to the class itself in the context of the class. The use case for this is to have the class name appear exactly once per class declaration.