Call a method by function name as string, not as the function itself

As the question suggests, I am trying to find out a way to call a method/function by passing in the string of the function name, instead of the function itself. Reflect.callMethod requires the function itself, but is it possible to call it some other way?

public static inline function colorByName(name:String):Color
    {
        
    }
    
    //Colors taken from MSDN: https://msdn.microsoft.com/en-us/library/system.drawing.color%28v=vs.110%29.aspx
    
    public static inline function aliceBlue() return new Color(0xF0, 0xF8, 0xFF);
    public static inline function antiqueWhite() return new Color(0xFA, 0xEB, 0xD7);
    public static inline function aqua() return new Color(0x00, 0xFF, 0xFF);
    public static inline function aquamarine() return new Color(0x7F, 0xFF, 0xD4);
    public static inline function azure() return new Color(0xF0, 0xFF, 0xFF);
    public static inline function beige() return new Color(0xF5, 0xF5, 0xDC);
    public static inline function bisque() return new Color(0xFF, 0xE4, 0xC4);
    public static inline function black() return new Color(0x00, 0x00, 0x00);

Here is a small sample of a file I have in my openfl-gui API called Color.hx including the function that would allow you to pass in a name for a color that returns that color by simply calling the static function. Is there a way to do this? So far, I have not found a way.

Okay, the following solution worked:

var fn:Dynamic = Reflect.field(Color, name);
var result = Reflect.callMethod(Color, fn, []);
if (result == null)
{
    trace("The name of the field " + name + " did not exist.");
    return null;
}
else
{
    if (Std.is(result, Color))
        return result;
    else
        return null;
}

Although apparently the Neko target doesn’t understand unsigned integers, unless my bitwise operation is wrong:

public function new(red:Int = 0x00, green:Int = 0x00, blue:Int = 0x00, alpha:Int = 0xFF) 
{
   valueWithAlpha = ((alpha << 24) | (red << 16) | (green << 8) | (blue << 0)) & 0xFFFFFFFF;
   value = ((red << 16) | (green << 8) | (blue << 0)) & 0xFFFFFF;
}

For the unsigned int I think you have to use <<< instead of << otherwise it uses twos compliments and turns it negative. In your case this would only make a difference with the alpha value.

Neko gets dodgy with 32-bit numbers, it like 31-bit better :confused:

Well, it’s unusual. If you do a trace on the Color instance, it will display two values, one positive and one negative. But if you pass in the value to the output directly, it outputs like an unsigned integer, no problem. At least neko is only ever used for testing, I suppose :wink: