-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Proposal: Derived class constructors and new.target #4609
Comments
It's currently not (except explicitly as As you say, it's a contrived example to demonstrate the issue, but just to elaborate: the example can be made to work trivially by moving class Abstract
@checkArgs = ->
throw new Error('BadArgs') if arguments.length is 0
constructor: (...args) ->
this.constructor.checkArgs(args)
this.args = args I suppose this case would become 'non-trivial' if the base class was from a library or something, and you wanted to defend it from invalid calls for whatever reason. |
Thanks for your insight and confirmation @connec 👍 You are right- in the case that if it were mandatory that the static method was to be called, you would define the reference to the static method in the base classes constructor, and subclasses could override the static method later. But if the static method was designed as an optional support method call, which is a valid use case, yes, you are right. One would be stumped without the new.target reference in CS2. In a way, a workaround like you suggested would mean defining an API by constraints of the language which is not unheard of, but too great either. The 'new.target` reference is unusual looking in ECMAScript as it is. Its difficult to see how it could be added to CS semantics unless the word 'new' was also to have another application? Then again, new.target is undefined anywhere else but in the constructor to the best of my knowledge. Perhaps it should just be a linguistic import into CS2. Thanks for the link to the discussion/issue and referencing this there btw. Hope the matter pings back on the radar. |
I think if we support it it will be as a direct import, with the necessary
leader/parser changes to pass it through. I wouldn’t expect it to be too
difficult, since `new.target` is currently a parse error. I may take a poke
at it soon if nobody else does!
|
Go forth and poke good sir; new.target beckons! |
It shouldn’t be that hard to at least update the compiler so that |
There are times when you may need to access static class members before super has been called:
In CoffeeScript2, the Abstract and Dense class implementations are fine
But none of the following implementations of Concrete are possible as new.target is not available:
The result is that Concrete must reference itself to access checkArgs, but this prevents subclasses from override the static checkArgs method.
If I'm not missing something, this looks like a bit of problem to me. How is it possible to reference the constructor and its properties before super in CS2?
The text was updated successfully, but these errors were encountered: