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
When you've got a bit of inheritance going on, there can be limits to what you can do with method chaining. Say you've got these two classes:
class Animal {
public sleep() : Animal {
return this;
}
}
class Elephant extends Animal {
public squirtWithTrunk(): Elephant {
return this;
}
}
Given an instance of Elephant, you could do this:
myElephant.squirtWithTrunk().sleep()
but you couldn't do this:
myElephant.sleep().squirtWithTrunk()
Because Animal doen't have a "squirtWithTrunk" method.
Seems fair enough, but given how much code space method chaining can save, and how much that can matter in JavaScript, it's a shame. And obviously in raw JavaScript this sort of thing just works.
If the following syntax were allowed, and "this" could be passed through a method chain without its type being narrowed to ancestor types, I think that might be quite nice:
class Animal {
public sleep() : this {
}
}
class Elephant extends Animal {
public squirtWithTrunk(): this {
}
}
I don't know how feasible that would be from a compiler point of view, but as a user of the language I'd find it very useful. In the last code snippet I imagine that the return statement would be disallowed, and that the returned value would always be the current object.
The text was updated successfully, but these errors were encountered:
When you've got a bit of inheritance going on, there can be limits to what you can do with method chaining. Say you've got these two classes:
Given an instance of Elephant, you could do this:
myElephant.squirtWithTrunk().sleep()
but you couldn't do this:
myElephant.sleep().squirtWithTrunk()
Because Animal doen't have a "squirtWithTrunk" method.
Seems fair enough, but given how much code space method chaining can save, and how much that can matter in JavaScript, it's a shame. And obviously in raw JavaScript this sort of thing just works.
If the following syntax were allowed, and "this" could be passed through a method chain without its type being narrowed to ancestor types, I think that might be quite nice:
I don't know how feasible that would be from a compiler point of view, but as a user of the language I'd find it very useful. In the last code snippet I imagine that the return statement would be disallowed, and that the returned value would always be the current object.
The text was updated successfully, but these errors were encountered: