thedayturns
thedayturns

Reputation: 10862

How can I get the name of a class from a static method on that class, in haxe?

Normally I would just use Type.getClassName(Type.getClass(this), but obviously that doesn't work because there is no this. Any ideas?

Upvotes: 2

Views: 750

Answers (1)

Andy Li
Andy Li

Reputation: 6023

If that is a static method, since there is no static member inheritance in Haxe, you already know what class it belongs to. So I would recommend hard-coding the class.

Or, you can use... macros!

import haxe.macro.Context;
import haxe.macro.Expr;
class ClassNameHelper {
    macro static public function getClassName():ExprOf<String> {
        return { expr: EConst(CString(Context.getLocalClass().toString())), pos: Context.currentPos() }
    }
}

class Test {
    public static function main() {
        trace(ClassNameHelper.getClassName()); //Test
    }
}

Upvotes: 6

Related Questions