Skip to content

"as" and "instanceof" are inconsistent when are used with impossible type #81

@m4tiz5zktonj

Description

@m4tiz5zktonj

Noticed the following during the refactoring:

import Toybox.Lang;

function f(x as Number) as Number {
    if ((x as String) == "") { // "The type Number cannot be converted to String because they have nothing in common" - OK
        return 0;
    } else if (x instanceof String) { // No warnings - NOK
        return x.length();
    } else {
        return x;
    }
}

PMC gives warning about bad cast x as String, but doesn't about bad condition x instanceof String. Is it possible to improve instanceof check to work the same way as the as?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions