Skip to content

[js] Type mismatch when using Bool default values #4793

@jgranick

Description

@jgranick

Using recent sources of the Haxe compiler, there appear to be mismatches when using Bool default values.

For example, there is an interface that defines a function with an optional Bool parameter. There is a class that implements the interface, which also has the optional Bool parameter.

Something like this:

interface ITest extends IAnotherInterface {

   function hello (world:String, cool:Bool = true):ISomeOtherInterface;

}

class TestImpl implements ITest {

    ...

    public function hello (world:String, cool:Bool = true):ISomeOtherInterface {

        ...

    }

}

When compiled, the compiler says that the class' default Bool value is really Null<Bool>, while the interface remains Bool. The types are not the same, and it throws a compile error.

If this were the real code, it would throw an error that String->Null<Bool>->ISomeOtherInterface should be String->Bool->ISomeOtherInterface and that it overrides the parent class (yes it uses the words "parent class") with the wrong type.

Interestingly (and lucky for me!) this does not occur if all parameters have a question mark. ?myValue:Bool = true is consistently Bool in all cases. It's using only myValue:Bool = true (which the code all originally uses) that results in the Null<Bool> issues.

Magically, this works:

interface ITest extends IAnotherInterface {

   function hello (world:String, ?cool:Bool = true):ISomeOtherInterface;

}

class TestImpl implements ITest {

    ...

    public function hello (world:String, ?cool:Bool = true):ISomeOtherInterface {

        ...

    }

}

I don't know if this would consistently happen all the time, or it's some combination of interfaces extending interfaces being implemented on classes extended by other classes. I don't know, but I wonder if the question mark exception here is intentional, or is how this issue was undetected (as I think all the Haxe test code uses question marks for optional values?)

The question marks should be optional, I am thankful for a workaround, but maybe you guys have an idea of the root cause? I believe it only affects 3.3 dev builds

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions