-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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 for Abstract Classes and Methods #3578
Comments
Thanks for that! |
I do not understand what you mean by this. |
Pretty much how I feel about it. I'd rather see a more lean functional-like TypeScript then adding astronaut like OO patterns like abstract or decorators. This overlaps tons with mixins or traits too (combining things from other things). The benefit of traits or mixins is you can design 'abstract patterns' that work with modules too. This proposal is limited to a 'class' which lets face it is really a function under the hood. |
@mhegazy This definitely needs re-wording. We want to prevent the constructor of an abstract class from being invoked. So we wanted to restrict the assignability of the constructor function. Consider the following example: class A {}
abstract class B extends A {}
var BB: typeof B = B;
var AA: typeof A = BB;
new AA; We don't want the second assignment to be legal. So we restrict the assignabilty of objects whose type is the type of an abstract class to other objects whose type is the type of an abstract class. I haven't yet implemented this as part of the attached PR. I've updated the spec to clarify this. |
@jbondc so true, inheritance (for the sake of code "reuse" or whatever else) is a hands down rotten idea, composition does it all with a little bit more effort, screw this s*t |
👍 |
💩 I'd love to hear how this aligns with the design goals:
|
I have long argued of the merits of embracing the protypical nature of ECMAScript instead of projecting a traditional OO paradigm. On the other hand, that hasn't stopped the wider community rejecting that and ECMAScript has taken a large bend towards traditional OO with ES6, though there seems much debate on this, as several of our ECMAScript luminaries seems to be conflicted on every aspect of this. @jbondc I think it is a fair challenge to question this proposal in line with design goals 5 and 6. It is very likely that some sort of abstract/traits/composition type features will end up in ES, though there seems to be no common consensus. Both abstract and traits were proposed during the class discussions in ES6/2015, but all got dropped because no one could agree. TypeScript already has interfaces which provide a sufficient level of abstraction ahead of any implementation of something along this line of ES. The traits discussion (#311) was closed by @RyanCavanaugh because of this conflicting with goal 6. I quote:
I honestly don't see what the difference is here.
While I am personally a fan of compostability, a strong emotive response without well thought arguments is unlikely to further your thoughts, especially when it appears you want to throw your arms up in the air and walk away. I don't think that is your intent, but that is the way it comes across. |
Here is the statement: inheritance is a dangerous tool that causes more problems than solves
All this contributes to frustration, missed deadlines, and myths that coding is hard. In my experience nothing is hard if designed properly. Well, inheritance encourages bad design by allowing developers to violate:
Besides that
Besides that, there is quite a number of successful examples of languages without inheritance at all:
|
Alright, while we'd love to have a constructive conversation on the merits of different approaches in software design, I believe that classes are a useful abstraction and the Unless there are specific "gotchas" introduced in our language from |
Objective thinking: What bothers me the most is the increasing and consistent sidestepping of the 'TS design rules' in favor of:
|
@kitsonk I must have missed it, when was abstract ever proposed? |
Here's the justification as I see it:
|
Don't think it is fair to call It "the Microsoft way" because it is a really a much requested feature here on GitHub (#6 (comment)) and on CodePlex. So some might not like it but others do. It is impossible to keep everyone happy after all. |
@RyanCavanaugh Yes because there's no multiple inheritance in Java or C#? If people prefer a better horse, fine. I'll take the car. |
Odersky et al's Programming in Scala recommends that, when you doubt, you use traits. What's the Microsoft philosophy? |
It's not useful to argue the merits of inheritance in this thread. Inheritance is part of JavaScript. TypeScript supports JavaScript. Done. Will TypeScript support other modeling methods, like composition via traits? I suspect it will at some point, especially since the JS committee is working on things like mixins. We'll support them as they land in JS. Let's keep the conversation here to the merits of 'abstract' as a useful concept to help TypeScript support JavaScript's already existing inheritance functionality. |
I see, so its not useful to discuss progress. Will be my last comment but will address the @RyanCavanaugh philosophy:
It's called a selection bias.
There's something called progress and believe it applies to programming languages.
Just because people have learned or been told to think a certain way and
Cool we agree. Next up const, readonly, sealed right?
Good, not baffled that you are baffled. My thinking/ideology is different then yours, clearly I must be evil or of have some "ill-faith", burn the heretics! |
I'm not sure why you feel so impassioned against a feature you are sure you will never use. Progress is a relative term, and while you may not find your domain is sufficiently serviced by a paradigm, consider the importance of being open minded in building a language - others have built well-thought-out designs using such paradigms and benefited greatly from them. Furthermore, ad hominems like saying we are going with the "Microsoft way" is non-constructive (if not just insulting). Let's try to keep a more tempered discussion on this issue. |
While I feel as impassioned about that wrangling purist-OO paradigms into ECMAScript is "bad", I agree with @DanielRosenwasser that laying it at the feet of "Microsoft way" diminishes all the hard work the TypeScript team have done. They strike me as a group of people who try their hardest to do the right thing, and try to limit external commercial imperatives that might influence them. |
I've retracted some of my comments, no intent to insult anyone and the fantastic job by the TS team. Learned a lot from the compiler, continue to learn and its a real work of art. Got carried away by the "question the good faith of someone", apologies for that. |
No worries; text is a lossy format for tonality and intent 😄. Everyone in this discussion is well-intentioned. Even if it's not coming off that way, know that we understand your concerns and value them. Keep giving us feedback - it's a learning experience for us too. (Also we appreciate the kind words guys!) |
Abstract support will never hurt anyone. If you don't want to use it, don't. Nobody's forcing you to use it. Assuming that this won't require re-architecting the project much (I doubt it will), I see absolutely no reason not to implement this feature. |
@aozgaa Why the close? |
@Aleksey-Bykov Many developers think this is a useful feature. You can create a fork without abstract support, stay on an old version, or just don't use abstract support. Nobody is making you use it. |
Just another thing to throw to garbage at linting and the rule for it is
|
Algebraic data types and pattern matching is not even in the road map.
|
Looks like C# is going to get them sooner than we here. Thats just plain
|
@Aleksey-Bykov, I'd actually love to have mixins as a feature, but can't comprehend doing it without structured type support. Our codebase is far too big and our developers too few to do much meaningful development without the benefits typing brings. Have you started submitting a proposal for mixins with typescript? If you haven't, are there good references to prior work where someone might begin working on fleshing that out? At this point if there's no proposals from TC39 which have stuck, it might as well start it's life here on the Typescript project |
@Gaelan You're totally right! |
👍 thank you. A right tool for the right job. |
@Aleksey-Bykov C# operates under a completely different set of constraints than TypeScript does. We could love pure functional programming more than anyone but it doesn't change the rules we actually need to operate with as we design for the future (ie conforming to future ECMAScript standards). If you want an arbitrary set of language features/semantics that compile to JS there are a lot of options, but TypeScript isn't one of them. There are also web centric languages that have opted to add desirable expression level features divorced from ECMAScript standards. The value/success of these two divergent strategies is left as an exercise for the reader ;) Suffice to say, we love designing new language features and want to keep adding stuff that makes everyone's lives easier, but it's simply not the case that we get to just pick our favorite features from Haskell, Smalltalk, etc and put them into TypeScript with no regard for future ECMAScript incompatibility they may/will cause. |
Great job! |
@Yves57 this is by design. there is no way to currently to distinguish between a property accessor and a property declaration, so a derived class can define a property declaration and would meet abstract constraints, but probably this is not what you intended by making the accessor abstract in the base. |
@mhegazy To be more precise, my case is the following
But I understand that this is a specific case! |
@Yves57, I've faced exactly same problem. Looks like this case isn't so rare... It would be better if abstract class was allowed to miss fields from it's interface (both value properties and methods), like in other OOP languages. interface A { p; m(); }
abstract class B implements A {}
class C extends B { get p() {...}; set p(v) {...}; m() {...} } But currently it gives:
|
Also I think that abstract value properties should be allowed in abstract classes (same way value properties are now allowed in interfaces): abstract class A {
abstract p;
}
class B extends A {
get p() {...}
set p(v) {...}
} |
@denvned can you file separate issues to track the two requests, 1. abstract properties, and 2. missing property declaration in abstract class implementing interfaces. |
I'm trying to use this in WebStorm 11 with TS1.6... Not working? |
@electricessence WebStorm doesn't use our language service, so unless this is an issue you're seeing at compilations (i.e. not while editing) you should probably check in with them. Though, it looks like they should support it: http://blog.jetbrains.com/webstorm/2015/09/webstorm-11-eap-142-4723/ |
@bryanerayner +1 for:
|
@bryanerayner, @AJamesPhillips mixins are evil, choose composition: https://medium.com/@dan_abramov/mixins-are-dead-long-live-higher-order-components-94a0d2f9e750#.usm8t3fuz |
@Aleksey-Bykov agreed, I shouldn't have said mixins as practically they share major downsides with multiple inheritance. For the moment I'm:
Would be very interested to see composition / stamps / aspect / traits, in TypeScript. And thanks for the link to Dan Abramov's article. |
It works in 11. From: Daniel Rosenwasser [mailto:[email protected]] @electricessence https://github.com/electricessence WebStorm doesn't use our language service, so unless this is an issue you're seeing at compilations (i.e. not while editing) you should probably check in with them. Though, it looks like they should support it: http://blog.jetbrains.com/webstorm/2015/09/webstorm-11-eap-142-4723/ — |
Proposal for Keyword
abstract
This is a proposal for a new keyword,
abstract
, to allow for incomplete/partially implemented classes.Introduction
This proposal expands upon #6, #2946, #2947.
Consider the following situation. A user would like to create a base class that shouldn't be instantiated as a template for derived classes. S/he might write something like
Today, the writer is forced to make a choice: either (i) Animal can be declared an interface, but then
yearsLeft
cannot have an implementation, or (ii) write the program as above, but then Animal can be instantiated andmakeSound
has a bogus implementation! Neither of these options is particularly attractive.We propose
abstract
as a class declaration modifier to allow the programmer control over whether a class can be instantiated, and as a member function modifier to control whether said member function offers an implementation (and whether the enclosing class is abstract).Details
The modifier
abstract
can prepend a class declaration or a member function declaration. Constructors cannot be declared abstract.typeof C
whereC
is anabstract
-declared class (hereafter abstract class) cannot be instantiated via calls tonew
.abstract
does not have an implementation.super.foo()
wherefoo
is an abstract method belonging to the parent class.this.foo()
wherefoo
is an abstract method belonging to that class.abstract
andstatic
.abstract
andprivate
.abstract
keyword.The following code snippet summarizes valid and invalid usages of the
abstract
keywordPros
abstract
annotations clarify the role of base classes in various patterns.Cons
To Be Discussed
Proposed Changes to Grammar
A.6 Classes
ClassDeclaration:
abstractopt class Identifier TypeParametersopt ClassHeritage { ClassBody }
MemberFunctionDeclaration:
MemberFunctionOverloadsopt MemberFunctionImplementation
AbstractMemberFunctionOverloads
AbstractMemberFunctionOverloads:
AbstractMemberFunctionOverload
AbstractMemberFunctionOverloads AbstractMemberFunctionOverload
AbstractMemberFunctionOverload:
AccessibilityModifieropt abstract PropertyName CallSignature ;
A.10 Ambients
AmbientClassDeclaration:
abstractopt class Identifier TypeParametersopt ClassHeritage { AmbientClassBody }
AbstractOrStatic:
abstract
static
AmbientPropertyMemberDeclaration:
AccessibilityModifieropt AbstractOrStaticopt PropertyName CallSignature ;
EDIT: updated to clarify the assignability of objects of "abstract type".
The text was updated successfully, but these errors were encountered: