-
Notifications
You must be signed in to change notification settings - Fork 383
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support instanceof checks for exported Scala class constructors #4062
Comments
To summarize our conversation on Gitter: It is possible for us to do so by setting The downside of this is that we would lock-in certain structural constraints on how Scala.js implements Scala classes in JavaScript. @sjrd does that summarize the discussion well? |
Yes, that is a fairly accurate summary. I'll add that I think the most promising way to implement this is on top of Symbol.hasInstance, although that means it would only work when emitting ECMAScript 2015 code. |
Oh, also to double check my understanding: The reason we are reluctant to support case classes that extend |
This is interesting now that I think about this: Wouldn't this give us a way forward if we chose a different encoding? In ES5.1 we could rely on |
That's right. And if
In theory, yes. In practice, that would mean more code paths if we change the encoding for ES6 but we have to preserve the old one for ES5.1. Another problem with @JSExportTopLevel("Foo")
@JSExportAll
class Foo(val x: Int) it would be rougly translate as class $c_Foo extends $c_jl_Object {
constructor(x) {
super()
this.f_x = x;
}
get "x"() {
return this.f_x;
}
}
function Foo(x) {
return new $c_Foo(x);
}
Foo.prototype = $c_Foo.prototype; which means that the following user JS code would happen to "work": class Bar extends Foo {
constructor(x, y) {
super(x);
this.y = y;
}
}
var bar = new Bar(5, 6)
bar.x // 5
bar.y // 6 So it would appear that one can meaningfully extend an exported class, even though that's completely undefined behavior, and will break in obscure situations (not final anymore, 2 reachable constructors, etc.). |
Maybe then we should just make it an ES2015 feature only. We do have precedent for this with static member inheritance, right? |
Yes, that's right. |
I dug up ce17362 , in which we actually removed that (then unspecified) behavior. 🤷♂️ |
At the moment,
instanceof
checks do only work for classes that extendjs.Object
. Unfortunately case classes must not extendjs.Object
.Motivation (copied from Gitter): I would like to define Scala ADTs (represented by sealed traits plus cases classes) by TypeScript union types. A common pattern for exhaustiveness checks in TypeScript is to use flow typing with instanceof checks.
The text was updated successfully, but these errors were encountered: