Title: Allow classes to be parametric in other parametric classes · Issue #1213 · microsoft/TypeScript · GitHub
Open Graph Title: Allow classes to be parametric in other parametric classes · Issue #1213 · microsoft/TypeScript
X Title: Allow classes to be parametric in other parametric classes · Issue #1213 · microsoft/TypeScript
Description: This is a proposal for allowing generics as type parameters. It's currently possible to write specific examples of monads, but in order to write the interface that all monads satisfy, I propose writing interface Monad
Open Graph Description: This is a proposal for allowing generics as type parameters. It's currently possible to write specific examples of monads, but in order to write the interface that all monads satisfy, I propose wri...
X Description: This is a proposal for allowing generics as type parameters. It's currently possible to write specific examples of monads, but in order to write the interface that all monads satisfy, I propose...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/1213
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Allow classes to be parametric in other parametric classes","articleBody":"This is a proposal for allowing generics as type parameters. It's currently possible to write specific examples of monads, but in order to write the interface that all monads satisfy, I propose writing\n\n``` javascript\ninterface Monad\u003cT\u003c~\u003e\u003e {\n map\u003cA, B\u003e(f: (a: A) =\u003e B): T\u003cA\u003e =\u003e T\u003cB\u003e;\n lift\u003cA\u003e(a: A): T\u003cA\u003e;\n join\u003cA\u003e(tta: T\u003cT\u003cA\u003e\u003e): T\u003cA\u003e;\n}\n```\n\nSimilarly, it's possible to write specific examples of cartesian functors, but in order to write the interface that all cartesian functors satisfy, I propose writing\n\n``` javascript\ninterface Cartesian\u003cT\u003c~\u003e\u003e {\n all\u003cA\u003e(a: Array\u003cT\u003cA\u003e\u003e): T\u003cArray\u003cA\u003e\u003e;\n}\n```\n\nParametric type parameters can take any number of arguments:\n\n``` javascript\ninterface Foo\u003cT\u003c~,~\u003e\u003e {\n bar\u003cA, B\u003e(f: (a: A) =\u003e B): T\u003cA, B\u003e;\n}\n```\n\nThat is, when a type parameter is followed by a tilde and a natural arity, the type parameter should be allowed to be used as a generic type with the given arity in the rest of the declaration.\n\nJust as is the case now, when implementing such an interface, the generic type parameters should be filled in:\n\n``` javascript\nclass ArrayMonad\u003cA\u003e implements Monad\u003cArray\u003e {\n map\u003cA, B\u003e(f: (a:A) =\u003e B): Array\u003cA\u003e =\u003e Array\u003cB\u003e {\n return (arr: Array\u003cA\u003e) =\u003e arr.map(f);\n }\n lift\u003cA\u003e(a: A): Array\u003cA\u003e { return [a]; }\n join\u003cA\u003e(tta: Array\u003cArray\u003cA\u003e\u003e): Array\u003cA\u003e {\n return tta.reduce((prev, cur) =\u003e prev.concat(cur));\n }\n}\n```\n\nIn addition to directly allowing compositions of generic types in the arguments, I propose that typedefs also support defining generics in this way (see [issue 308](https://github.com/Microsoft/TypeScript/issues/308)):\n\n``` javascript\ntypedef Maybe\u003cArray\u003c~\u003e\u003e Composite\u003c~\u003e ;\nclass Foo implements Monad\u003cComposite\u003c~\u003e\u003e { ... }\n```\n\nThe arities of the definition and the alias must match for the typedef to be valid.\n","author":{"url":"https://github.com/metaweta","@type":"Person","name":"metaweta"},"datePublished":"2014-11-19T18:46:02.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":189},"url":"https://github.com/1213/TypeScript/issues/1213"}
| 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:3cf12a50-e9ac-9660-3eba-b6983e9481ae |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CD0A:1CC65A:B3B0A3:EF9E08:6A62B537 |
| html-safe-nonce | 4e7860a426cff2f9bd2c75dd78b8c9e41324154fc64ce9eb7d46cb61ea1de7ad |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDRDBBOjFDQzY1QTpCM0IwQTM6RUY5RTA4OjZBNjJCNTM3IiwidmlzaXRvcl9pZCI6IjE2NjEwMTc5MTk5MTk5Mjg2MzEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 7683420d0792006bf5c89508fe63bc762ac87d75d330b7cb60267672ca9b6b69 |
| hovercard-subject-tag | issue:49436743 |
| 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/1213/issue_layout |
| twitter:image | https://opengraph.githubassets.com/61091f5fde5f7fd5ea3b3274009e397b0d6fa73af13deb895284b68eb714c5f6/microsoft/TypeScript/issues/1213 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/61091f5fde5f7fd5ea3b3274009e397b0d6fa73af13deb895284b68eb714c5f6/microsoft/TypeScript/issues/1213 |
| og:image:alt | This is a proposal for allowing generics as type parameters. It's currently possible to write specific examples of monads, but in order to write the interface that all monads satisfy, I propose wri... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | metaweta |
| hostname | github.com |
| expected-hostname | github.com |
| None | 2738d7125e864b55224a8bc40c72c220ca7d2c73f91a7bb8d69dffe9d35e997d |
| 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 | bf54af498459157b66a67136f992a68752cdbb54 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width