Skip to content

Constructors for some DOM types may simply throw. #53

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

Closed
joshualitt opened this issue Jun 15, 2023 · 7 comments
Closed

Constructors for some DOM types may simply throw. #53

joshualitt opened this issue Jun 15, 2023 · 7 comments

Comments

@joshualitt
Copy link
Contributor

Currently, there are many constructors that throw. For example, web.HTMLDivElement(). We should either lower these like dart:html to document.createElement, or we should find some way to disallow invoking the constructor statically, i.e. a lint.

@eernstg
Copy link
Member

eernstg commented Jun 16, 2023

Would abstract class work?

@joshualitt
Copy link
Contributor Author

Hmm, maybe it would? Would you prefer a magic constructor that calls document.createElement or should we make it a static error to use the constructor?

@eernstg
Copy link
Member

eernstg commented Jun 16, 2023

I was just suggesting abstract because that seemed to be a good fit for one of the approaches that you mentioned: 'disallow invoking the constructor statically'. That would prevent instance creations and still allow superinitializations in other constructors (but it looks like there are no subclass relationships, only implements, so we can't have super(...) anyway). On the other hand, if document.createElement("Something") can actually be called with an argument that yields a useful outcome then that sounds more pragmatic.

In any case, I just noticed the language perspective on this question—it would a serious mistake to assume that I can say anything useful about web programming. ;-)

@sigmundch
Copy link
Member

What if we don't emit them?

We may get issues today in our current staticInterop implementation, so we may need to emit a private unaccessible constructor instead? (dart:html does both that and the factory that uses createElement) to make sure the classes remain sealed.

Howevr, once we switch to inline-classes, does this problem go away and we can simply omit a constructor altogether?

@eernstg
Copy link
Member

eernstg commented Jun 23, 2023

we can simply omit a constructor altogether?

I would think so.

@sigmundch
Copy link
Member

I ran again into these, so I'd suggest we do make a change to make these stand out more easily:

  • add a _ constructor that throws. This changes the default behavior and removes the default constructor from our classes:
@staticInterop @JS
class HTMLDivElement ... {
  HTMLDivElement._() => throw "unsupported";
}
  • once we have the new language syntax, we can leave them out entirely.
  • once the language introduces static extension members, we can introduce factories that call document.createElement from the glue layer.

@srujzs
Copy link
Contributor

srujzs commented Feb 29, 2024

Handled in #185.

@srujzs srujzs closed this as completed Feb 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants