Skip to content
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

CS2 Discussion: Output: Super - CS vs ES6 semantics #4961

Closed
coffeescriptbot opened this issue Feb 19, 2018 · 6 comments
Closed

CS2 Discussion: Output: Super - CS vs ES6 semantics #4961

coffeescriptbot opened this issue Feb 19, 2018 · 6 comments

Comments

@coffeescriptbot
Copy link
Collaborator

From @mrmowgli on 2016-12-28 13:01

Currently we have classes working in connec's branch pull #4354 (read the comments) - however as we go through the tests and compatibility with ES6 classes there are a lot of edge cases around super. In particular how ES6 (currently) handles super.

The basics of ES and super:

  • super() needs to be called in a constructor for this to exist
  • super() can't be used in an bound ES constructor function () =>
  • super can't be called from an external constructor
  • super can't be called more than once in a constructor
  • super can be called in overridden methods, the syntax becomes super.methodName()

This leads to some definite breaks in how the original CS classes operated, but probably the biggest restriction is the first one: super() needs to be called in a constructor for this to exist. The reason this becomes an issue is a pretty common use case: @ parameters.

For example the following code won't compile:

class A

class B extends A
  constructor: (@name) ->

This is because to refer to @name we actually have to make a super() call. connec played with the idea of automatically inserting a super() in the generated code, which required hoisting it to the top of the method. This unfortunately means that a parent constructor is getting called without your knowledge, leading to unpredictable inheritance. You might not want to call the original constructor, but by using @ parameters you inadvertently call into the parent, potentially without fulfilling it's signature requirements.

In addition, the following leads to a race condition:

class A
  constructor(@name)

class B extends A
  constructor: (@name) ->
    super(@name)   

since the @name parameter requires this within the super call.

I understand that sneaking a super() into the constructor transparently can be an issue, but is there a way to make this available in a subclass without super? Perhaps a blend of the original CS and ES6?

I personally would want to keep as much of the existing CS syntax as possible.

Thoughts?

@coffeescriptbot
Copy link
Collaborator Author

From @triskweline on 2016-12-28 13:35

Two more ES/super facts that are relevant to this decision:

  • If the constructor of a child class does not call super, the constructor is expected to return an object. The returned object is used in place of the object that was not constructed (sic).
  • If you neither call super in the constructor, nor return an object to use instead, a ReferenceError is thrown.

So we either must call super (by either automatically inserted or by forcing the developer to not forget) or support overriding of constructors.

@coffeescriptbot
Copy link
Collaborator Author

From @edemaine on 2016-12-29 12:38

Instead of automatically calling super, can we just set this to a blank object (of the right type... perhaps this is what @henning-koch means about constructor overriding) and return it at the end? At least for constructors in subclasses that don't call super...

@coffeescriptbot
Copy link
Collaborator Author

From @connec on 2016-12-29 19:07

@edemaine do you mean something like

class B extends A
  constructor: (@param) ->
    console.log @param

becoming

class B extends A {
  constructor (param) {
    var this1
    this1 = Object.create(B.prototype)
    this1.param = param
    console.log(this1.param)
    return this1
  }
}

?

@coffeescriptbot
Copy link
Collaborator Author

From @edemaine on 2016-12-29 19:16

@connec Yes, except I think you need to return this1 at the end.

@coffeescriptbot
Copy link
Collaborator Author

From @connec on 2016-12-29 19:20

Good point, updated for reference.

@coffeescriptbot
Copy link
Collaborator Author

From @GeoffreyBooth on 2017-02-04 20:10

Closed via #4424.

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

1 participant