0

Haxe 2.09 can not compile this:

["Zebra", "Gnu", "Elephant"].map(callback(String.toLowerCase));

It complains with this error:

String has no field toLowerCase

Though it'd be easier to write that line without using Lambda.map, but I'd really want to start using the callback feature as it seems very suited to Lambda coding, as explained here: http://haxe.org/ref/callback

I guess it has to do with the fact the String.toLowerCase method is not static. The example given by the doc is :

callback(Math.pow, 3).

But Math.pow() is static...

Would you know any way to make it work?

the_yellow_logo
  • 675
  • 5
  • 13

2 Answers2

4

You are correct, it's because it's not static, toLowerCase is a method that is defined on each instance - therefore you can't bind it to a whole bunch of different strings.

Your solution (creating a static function) is fine, you can also just use a very simple function inline:

["Zebra", "Gnu", "Elephant"].map(function (str) return str.toLowerCase());
Jason O'Neil
  • 5,908
  • 2
  • 27
  • 26
0

I ended up creating a function to my StringExt class.

["Zebra", "Gnu", "Elephant"].map(callback(StringExt.lower));

And in StringExt:

public inline static function lower(s:String):String { return s.toLowerCase(); }

Not sure if there is a better way.

the_yellow_logo
  • 675
  • 5
  • 13