tc39 / proposal-optional-chaining

Home Page:https://tc39.github.io/proposal-optional-chaining/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Should we support class inheritance?

bcoe opened this issue · comments

I was putting some work into enumerating grammar productions today, and one jumped out at me:

ClassHeritage[Yield, Await]:
  extends LeftHandSideExpression[?Yield, ?Await]

It feels a bit weird for someone to be able to conditionally inherit:

class Cat extends biology?.Animal {
  // ☝ why would I ever do this? should it be an early error?
}

Should this be an early error, or are folks getting the behavior they deserve for using optional chaining in a silly way?

What makes things worse is imagine this:

const biology = {
  Animal: null
}
class Cat extends biology?.Animal?.foo {
  // this is an exception because ?. of null returns undefined.
}
class Dog extends biology.Animal {
  // this is not an exception, as null can be extended.
}

I think that you have a good test, but I don't think we need to go overboard with restricting the productions here. It's nonsense, and very rare, probably intentional, nonsense. 😃

I tend to agree, @littledan made a similar point for another edge-case, along the lines of:

It's not our place to protect users from every weird edge-case they can create with the language.

And my example is wrong, you'd have to do something even weirder to get an exception:

class Cat extends biology?.Animal?.a {
  // I believe biology?.Animal would be fine.
}

Note that you can already write nonsense like class foo extends new bar { } or class foo extends (a + b) { }. I don’t think that optional chaining merit more attention than any other random expression.

Today it can be determined statically, by the presence of the keyword extends, whether a class is derived (which in practice means: whether you must use super() in the constructor in order to construct this). If we support “optional extends” that won’t be true any longer. I do not prospect anything in that direction.