You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Consistently set defaults for new-based constructorsΒ #624
As a slight inconvenience, because the fields are initialized in the trait, the override modifier is required.
But what doesn't work is overriding the vars: scalac complains mutable variable cannot be overridden.
changed the title [-]MutationObserverInit: new-based constructor will not compile[/-][+]Consistently set defaults for `new`-based constructors[/+]on Nov 9, 2021
But I can see why we have it done this way: for a ResponseInit, it doesn't make sense to omit any of the fields; while for the MutationObserverInit, having most of the values set to undefined is okay and intended.
Activity
sjrd commentedon Nov 9, 2021
This is intentional. The intended way to use it is as follows:
yurique commentedon Nov 9, 2021
Ahh, indeed! My bad! Feeling kinda stupid now =)
Thank you, SΓ©bastien!
yurique commentedon Nov 9, 2021
I think this was what had thrown me off:
armanbilge commentedon Nov 9, 2021
Hmm, is the use of var not needed there? I grabbed that example from one of my own projects:
https://github.com/http4s/http4s-dom/blob/b900066aec70b39286925684b9501e66b36c11fe/dom/src/main/scala/org/http4s/dom/ServiceWorker.scala#L92-L95
Update: I tried removing it, but it didn't compile, so idk π
yurique commentedon Nov 9, 2021
@armanbilge Here's how I understand it :)
ResponseInitis defined like this (vars are abstract):and this compiles (overriding-implementing the vars from the trait):
without putting
vars there it won't compile because those need to be overriden (implemented).By contrast,
MutationObserverInitis defined like this (there are default implementations for the vars):and scalac won't allow overriding them (vars) β I think this was the first time I ever tried to override a var β so the way to initialize them is as SΓ©bastien suggested: by creating a new instance (with default initial values for those vars) and then imperatively re-assigning those vars in the constructor:
The benefit of it is we don't have to specify(implement) all the vars, only those that we care about.
armanbilge commentedon Nov 9, 2021
Aha, excellent clarification! Thank you.
Seems like we need to be more consistent with this? I.e.
ResponseInitshould be changed to usejs.undefined.[-]MutationObserverInit: new-based constructor will not compile[/-][+]Consistently set defaults for `new`-based constructors[/+]yurique commentedon Nov 9, 2021
Consistency would be nice!
But I can see why we have it done this way: for a
ResponseInit, it doesn't make sense to omit any of the fields; while for theMutationObserverInit, having most of the values set toundefinedis okay and intended.armanbilge commentedon Nov 9, 2021
I thought the same, so I checked MDN and in fact their example omits the
headers! Which is impossible with ours.https://developer.mozilla.org/en-US/docs/Web/API/Response/Response#examples
Given that this inconsistency can be confusing/surprising, I think we should change this.
Also, not necessarily all fields have to default to
js.undefined, we can do that only for optional fields.yurique commentedon Nov 9, 2021
Indeed. Interesting π€.
Well, I'm really not sure what the best way to approach this is. A change like this would be a breaking change, at the very least.
armanbilge commentedon Nov 9, 2021
Source breaking but not binary-incompatible, so if we decide to go forward with it, it can land in 2.1.0.