Skip to content

Proposal: __inherits__ keyword #270

Open
@nyuichi

Description

@nyuichi
/*
 * Proposal: __inherits__ keyword
 */
native class Duplex implements Readable __inherits__ Writable {
  // pass
}

/*
 * Only native classes are allowed to use __inherits__ keyword.
 * An "inherits" takes some native classes as RHS values.
 * Instance objects of A class implicitly cast up to B or C.
 */
native class A __inherits__ B, C {
}

/*
 * if as operator is applied against any "inherited" class,
 * and --enable-type-check flag is enabled, the expression expands into
 * a combination of some instanceof expressions. this behavior depends 
 * largely on static linkage, which is not guaranteed to keep in future 
 * language spec.
 */
 obj as B

 => (function ($obj) {
        // thanks to static-linkage, the compiler can list up
        // all classes "inherited" from B
        if (! ($obj instanceof B || $obj instanceof A)) {
            throw new Error("runtime type error");
        }
        return $obj;
    }($obj))

/*
 * Whether we should change the behavior of instanceof expression or not
 * is another point. Basically I'm against to the idea to change the current
 * way (i.e. let instanceofs return true when the LHS expression is of the type A
 * and the RHS is either of B or C). But if you mind the asynmmetry "as" and "instanceof"
 * it is possible change their behaviors to the same.
 */
 obj instanceof B

 => false or true?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions