Title: Proposal for Abstract Classes and Methods · Issue #3578 · microsoft/TypeScript · GitHub
Open Graph Title: Proposal for Abstract Classes and Methods · Issue #3578 · microsoft/TypeScript
X Title: Proposal for Abstract Classes and Methods · Issue #3578 · microsoft/TypeScript
Description: 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...
Open Graph Description: 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. Consid...
X Description: 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. Consid...
Mail addresses
notifications@github.com
Opengraph URL: https://github.com/microsoft/TypeScript/issues/3578
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Proposal for Abstract Classes and Methods","articleBody":"# Proposal for Keyword `abstract`\n\nThis is a proposal for a new keyword, `abstract`, to allow for incomplete/partially implemented classes.\n# Introduction\n\nThis proposal expands upon #6, #2946, #2947.\n\nConsider 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\n\n``` ts\nclass Animal {\n public age : number;\n public yearsLeft() { return 20 - this.age; }\n public makeSound() : string { return \"???\"; }\n}\n\nclass Cow extends Animal {\n makeSound() { return \"Moo\"; }\n}\n\nclass Cat extends Animal {\n makeSound() { return \"Meow\"; }\n}\n```\n\nToday, 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 and `makeSound` has a bogus implementation! Neither of these options is particularly attractive.\n\nWe 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).\n# Details\n\nThe modifier `abstract` can prepend a class declaration or a member function declaration. Constructors cannot be declared abstract.\n- Objects whose type is `typeof C` where `C` is an `abstract`-declared class (hereafter abstract class) cannot be instantiated via calls to `new`. \n- A member function declared `abstract` does not have an implementation.\n- Abstract methods cannot be called within a class member function body via `super.foo()` where `foo` is an abstract method belonging to the parent class.\n- Abstract methods may be called within a class member function body via `this.foo()` where `foo` is an abstract method belonging to that class.\n- If a class has any abstract methods, then the class itself must be declared abstract. \n- Abstract methods are inherited. \n- Abstract methods can be overridden in the derived class, either by an abstract method (effectively a re-declaration) or a function declaration that offers an implementation. \n- A method cannot be simultaneously `abstract` and `static`.\n- A method cannot be simultaneously `abstract` and `private`.\n- If an abstract member declaration contains an explicit accessibility modifier, then the modifier must precede the `abstract` keyword.\n- Overloads on a member function must be either all abstract or all not abstract.\n- Overloads of abstract member functions must be declared adjacent to eachother.\n- Objects whose type is the type of an abstract class can only be assigned to other objects whose type is the type of an abstract class. For example, \n\nThe following code snippet summarizes valid and invalid usages of the `abstract` keyword\n\n``` ts\n\nclass A {\n // ...\n}\n\nabstract class B {\n foo(): number { return bar(); }\n abstract bar() : number;\n}\n\nnew B; // error\n\nimport myB = B;\nnew myB; // error\n\nvar BB: typeof B = B;\nvar AA: typeof A = BB; // error, AA is not of abstract type.\nnew AA;\n\nfunction constructB(Factory : typeof B) {\n new Factory; // error -- Factory is of type typeof C\n}\n\nvar BB = B;\nnew BB; // error -- BB is of type typeof C\n\nvar x : any = C;\nnew x; // okay -- undefined behavior at runtime\n\n\nclass C extends B { } // error -- not declared abstract\n\nabstract class D extends B { } // okay\n\nclass E extends B { // okay -- implements abstract method\n bar() { return 1; }\n\nabstract class F extends B {\n abstract foo() : number;\n bar() { return 2; }\n}\n\nabstract class F {\n abstract qux(x : number) : string;\n abstract qux() : number;\n y : number;\n abstract quz(x : number, y : string) : boolean; // error -- declarations must be adjacent\n\n abstract nom() boolean;\n nom(x : number) boolean: // error -- use of modifier abstract must match on all overloads.\n}\n\nclass G { // error -- not declared abstract\n abstract baz() : number;\n}\n```\n# Pros\n- Allows the programmer to write partial implementations of a class.\n- Provides a mechanism for the type system to warn about classes that shouldn't be instantiated.\n- `abstract` annotations clarify the role of base classes in various patterns.\n# Cons\n- Increases the complexity of the language.\n# To Be Discussed\n- Should abstract be allowed within class expressions?\n# Proposed Changes to Grammar\n## A.6 Classes\n\n _ClassDeclaration_:\n abstract\u003csub>opt\u003c/sub> class _Identifier_ _TypeParameters_\u003csub>opt\u003c/sub> _ClassHeritage_ { _ClassBody_ }\n\n _MemberFunctionDeclaration_:\n _MemberFunctionOverloads_\u003csub>opt\u003c/sub> _MemberFunctionImplementation_\n _AbstractMemberFunctionOverloads_\n\n _AbstractMemberFunctionOverloads_:\n _AbstractMemberFunctionOverload_\n _AbstractMemberFunctionOverloads_ _AbstractMemberFunctionOverload_\n\n _AbstractMemberFunctionOverload_:\n _AccessibilityModifier_\u003csub>opt\u003c/sub> abstract _PropertyName_ _CallSignature_ ;\n## A.10 Ambients\n\n _AmbientClassDeclaration_:\n abstract\u003csub>opt\u003c/sub> class _Identifier_ _TypeParameters_\u003csub>opt\u003c/sub> _ClassHeritage_ { _AmbientClassBody_ }\n\n _AbstractOrStatic_:\n abstract\n static\n\n _AmbientPropertyMemberDeclaration_:\n _AccessibilityModifier_\u003csub>opt\u003c/sub> _AbstractOrStatic_\u003csub>opt\u003c/sub> _PropertyName_ _CallSignature_ ;\n\nEDIT: updated to clarify the assignability of objects of \"abstract type\".\n","author":{"url":"https://github.com/aozgaa","@type":"Person","name":"aozgaa"},"datePublished":"2015-06-19T22:51:15Z","url":"https://github.com/microsoft/TypeScript/issues/3578"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:152e7a47-d766-8720-fab7-edf4aa78a0ff |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9C6A:3F8AB1:2BB7942:378DDC9:6B019758 |
| html-safe-nonce | 15f804b3f47b18fa64e166792e75f5a63c2dd034b7e86cc95f215e3239d26a84 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QzZBOjNGOEFCMToyQkI3OTQyOjM3OEREQzk6NkIwMTk3NTgiLCJ2aXNpdG9yX2lkIjoiODEyNzY2ODIwMDAyOTY1NjkyMSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 01b9e64571cd2a9f137054e9287f4f3f346b85c1dd41178aaa02fe1c9b88b123 |
| hovercard-subject-tag | issue:89677065 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/3578/issue_layout |
| twitter:image | https://opengraph.githubassets.com/2d7ae77796649d3d593614c90c0193ff8cb01b5c986b6085b438728923ce5361/microsoft/TypeScript/issues/3578 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/2d7ae77796649d3d593614c90c0193ff8cb01b5c986b6085b438728923ce5361/microsoft/TypeScript/issues/3578 |
| og:image:alt | 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. Consid... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | aozgaa |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git |
| octolytics-dimension-user_id | 6154722 |
| octolytics-dimension-user_login | microsoft |
| octolytics-dimension-repository_id | 20929025 |
| octolytics-dimension-repository_nwo | microsoft/TypeScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 20929025 |
| octolytics-dimension-repository_network_root_nwo | microsoft/TypeScript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width