COMMUNITY

Default function arg values for abstracts

#1

I have abstract class declared like this:

abstract MyAbstractClass(Vector<Float>) from Vector<Float> to Vector<Float> {
....
    @:from
    public static function fromInt( value : Int ) : MyAbstractClass {
    ....
    }
....
}

This kind of code works (as intended):

var abc : MyAbstractClass = 12345;

but this kind of declaration doesn’t:

function aFunction( param : MyAbstractClass = 54321 ) {....}

with error being:

.... : Int should be MyAbstracClass

As one solution, I could have MyAbstractClass declared like this:

abstract MyAbstractClass(Int) from Int to Int { ... }

in which case I don’t get the error when declaring a function with default argument value. This is viable in my case but I would rather stick with Vector<Float>.

Is @from supposed to work with default value arguments or not? What am I missing here?

Is there some other way to tell Haxe to use implicit cast in case of default argument values when the value is not of the same type as argument and appropriate conversion method is provided?