How to identify the calling function

In function A there’s a call to function B. Is there a way to identify the name of function A within function B?

Thanks in advance!

This is typically not possible in many programming languages. You could include an extra argument in function B that function A can set to indicate that function B should behave differently.

function b(flag:Bool = false) {
	if (flag) {
		// special behavior for function a
	}
}

function a() {
	b(true); // called with flag
}

function c() {
	b(); // called without flag
}
import haxe.CallStack;

var callstack: Array<StackItem> = CallStack.callStack();

The calling method should be at the top of the callstack. Every element of the array is an enum so you’ll have to handle it in a switch-case.

1 Like

You can add PosInfos argument to B function:

public function b(?p:haxe.PosInfos) {
		trace(p.methodName);
}
2 Likes