COMMUNITY

“Cannot access reverse in static function”

Hello. As said earlier I’m a total newbie to Haxe. I tried concocting the following code from https://code.haxe.org/category/beginner/strings.html:

class Main {
	static public function main() {
		trace(reverse("Haxe is great!"));
	}

	function reverse(a:String):String {
		var arr = a.split('');
		arr.reverse();
		return arr.join('');
	}
}

Trying to do haxe --run Main.hx results in

Main.hx:3: characters 9-16 : Cannot access reverse in static function
Main.hx:3: characters 9-16 : For function argument 'v'

Adding static public to reverse doesn’t help any. Not sure what to do here. Please advise!

This is strange, static function reverse(...) should do the job. The error you’re getting is about accessing instance method reverse from static method main, which is not possible, because there is no instance in the static context :slight_smile:

Thought I should add if you want to use reverse as a method of Main rather than a static function.
https://try.haxe.org/#A3DaE

class Test {
	static public function main() {
        var m = new Test();
		trace(m.reverse("Haxe is great!"));
	}
	function new(){
        
    }
	function reverse(a:String):String {
		var arr = a.split('');
		arr.reverse();
		return arr.join('');
	}
}