Open FranklinWhale opened 5 years ago
This is the expected behavior, but we should probably have a better error in the simulator. The error thrown on hardware is 984 (invalid cast).
The issue here is that member access to string
, array
and object
are handled differently in PXT runtime and, when the type is an interface
, it is handled as if it is an object
and thus passing string
and array
into the function causes runtime error.
To make the function work at runtime, the showLength
function can be written as follows:
function showLength(x: Length) {
basic.showNumber(typeof x === "string" ? x.length : Array.isArray(x) ? (x as any[]).length : x.length)
}
Perhaps the error message may mention that an explict type assertion may be required.
Describe the bug In PXT 4.4, cast to class or interface is not supported. In PXT 5.5, no error is displayed on the JavaScript editor but the code does not run.
To Reproduce Steps to reproduce the behavior:
Expected behavior Either the cast is disallowed with error or it works successfully.