Title: Mixin language support (with toy implementation) · Issue #2919 · microsoft/TypeScript · GitHub
Open Graph Title: Mixin language support (with toy implementation) · Issue #2919 · microsoft/TypeScript
X Title: Mixin language support (with toy implementation) · Issue #2919 · microsoft/TypeScript
Description: Hello TypeScripters. I might be opening a can of worms which might bring an age of darkness upon us (but see PS). Anyway, I've done an attempt to add language support for mixins. For the user, it is similar to the extends or implements k...
Open Graph Description: Hello TypeScripters. I might be opening a can of worms which might bring an age of darkness upon us (but see PS). Anyway, I've done an attempt to add language support for mixins. For the user, it i...
X Description: Hello TypeScripters. I might be opening a can of worms which might bring an age of darkness upon us (but see PS). Anyway, I've done an attempt to add language support for mixins. For the user, ...
Mail addresses
notifications@github.com
Opengraph URL: https://github.com/microsoft/TypeScript/issues/2919
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Mixin language support (with toy implementation)","articleBody":"Hello TypeScripters.\n\nI might be opening a can of worms which might bring an age of darkness upon us (but see PS). Anyway, I've done an attempt to add language support for mixins.\n\nFor the user, it is similar to the `extends` or `implements` keywords, and thus goes by `mixes`. The semantic intent of the keyword is that a `Mixture` is NOT a derived class of its `Mixins`, it just copies their static and dynamic properties.\n\nSeveral mixins can be supplied, the order determines which property will finally be mixed (newer definitions shadow previous definitions).\n\nWhat this does is roughly equivalent to http://www.typescriptlang.org/Handbook#mixins, except there is no need to copy attributes manually, so mixins become more manageable - hopefully.\n\n``` ts\n\nclass Base {\n say(hello:string) {\n console.log(hello);\n }\n}\n\nclass Mixin1 {\n something : string = \"Couac couac\";\n\n fn1() {\n console.log(this.something);\n }\n}\n\n\nclass Mixin2 { \n something : string = \"shadowing you, little duck!\"; \n\n fn2() {\n console.log(\"The dragon is\", this.something);\n }\n}\n\n/* Only mixes one mixin */\nclass SimpleMixture mixes Mixin1 {\n\n}\n\n/* Mixes several mixins, property definitions in newer mixins (from left\nto right) shadow those from previous mixins AND those in the mixture\nclass itself, including inherited ones. */\nclass MultiMixture extends Base mixes Mixin1, Mixin2 {\n\n}\n\n\nvar sm1 = new SimpleMixture();\nsm1.fn1();\n\nvar sm2 = new MultiMixture();\nsm2.fn1();\nsm2.fn2();\n\nconsole.log( sm1 instanceof Base );\nconsole.log( sm1 instanceof Mixin1 );\nconsole.log( sm2 instanceof Base );\nconsole.log( sm2 instanceof Mixin2 );\n```\n\nWhich emits\n\n``` js\nvar __extends = this.__extends || function (d, b) {\n for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];\n function __() { this.constructor = d; }\n __.prototype = b.prototype;\n d.prototype = new __();\n};\nvar __applyMixins = this.__applyMixins || function (mixtureTarget) {\n for(var a=1; a\u003carguments.length; ++a) {\n var mixin = arguments[a];\n Object.getOwnPropertyNames(mixin.prototype).forEach( function(name) {\n mixtureTarget.prototype[name] = mixin.prototype[name];\n })\n }; \n};\nvar Base = (function () {\n function Base() {\n }\n Base.prototype.say = function (hello) {\n console.log(hello);\n };\n return Base;\n})();\nvar Mixin1 = (function () {\n function Mixin1() {\n this.something = \"Couac couac\";\n }\n Mixin1.prototype.fn1 = function () {\n console.log(this.something);\n };\n return Mixin1;\n})();\nvar Mixin2 = (function () {\n function Mixin2() {\n this.something = \"shadowing you, little duck!\";\n }\n Mixin2.prototype.fn2 = function () {\n console.log(\"The dragon is\", this.something);\n };\n return Mixin2;\n})();\n/* Only mixes one mixin */\nvar SimpleMixture = (function () {\n function SimpleMixture() {\n Mixin1.apply(this) ;\n }\n __applyMixins(/*mixtureTarget*/ SimpleMixture, Mixin1);\n return SimpleMixture;\n})();\n/* Mixes several mixins, property definitions in newer mixins (from left\nto right) shadow those from previous mixins AND those in the mixture\nclass itself, including inherited ones. */\nvar MultiMixture = (function (_super) {\n __extends(MultiMixture, _super);\n function MultiMixture() {\n _super.apply(this, arguments);\n Mixin1.apply(this) ;\n Mixin2.apply(this) ;\n }\n __applyMixins(/*mixtureTarget*/ MultiMixture, Mixin1, Mixin2);\n return MultiMixture;\n})(Base);\nvar sm1 = new SimpleMixture();\nsm1.fn1();\nvar sm2 = new MultiMixture();\nsm2.fn1();\nsm2.fn2();\nconsole.log(sm1 instanceof Base);\nconsole.log(sm1 instanceof Mixin1);\nconsole.log(sm2 instanceof Base);\nconsole.log(sm2 instanceof Mixin2);\n```\n\nand outputs\n\n``` bash\n>>> Couac couac\n>>> shadowing you, little duck!\n>>> The dragon is shadowing you, little duck!\n>>> false\n>>> false\n>>> true\n>>> false\n```\n\nAt this point it seems to work with target ES\u003c6 with quite some limitations (the user can't pass arguments to the mixin constructors). Type checking seems to be working (although I could not make Eclipse or Emacs behave with the new keyword). But be warned Very early experimental preview code from a bad JS programmer who knew nothing about TSC's internals three days ago and who was in a rush. Be warned.\n\nI did this to overcome a maintainability issue with my code and rushed this implementation. It was also an exercise to learn more about TSC. I'm also aware that it might not be a great idea to add keywords to a language each time one gets into trouble, but if others see interest in it or you have suggestions, the branch is here https://github.com/dbarbeau/TypeScript/tree/mixes. Just don't expect this implementation to be stable/complete/anything. Actually, if it can just spark some interest/discussion on how to make mixins more useable, then I'm ok with that.\n\nDaniel\nPS: Don't get mad at me :)\nPS: I'm now testing this with more \"serious code\", I will quickly see if it is indeed practical.\n","author":{"url":"https://github.com/dbarbeau","@type":"Person","name":"dbarbeau"},"datePublished":"2015-04-25T21:12:25Z","url":"https://github.com/microsoft/TypeScript/issues/2919"}
| 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:0be9c211-316a-8174-aa9a-fa5e766ac4af |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DA3A:3F8AB1:740E32:977E84:6B00D458 |
| html-safe-nonce | 44b12f2bfb9f226b045a7f427c9a6f26c183a4a5cdf1ccddb10ee00723069162 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEQTNBOjNGOEFCMTo3NDBFMzI6OTc3RTg0OjZCMDBENDU4IiwidmlzaXRvcl9pZCI6IjQxNTkxMjQwNTM1MjI2OTkzNTIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 6cab6dc4b2f0b236f8fa305a4b7355bb116cbfadceb1cc17aee71be7afa0ed5c |
| hovercard-subject-tag | issue:70967351 |
| 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/2919/issue_layout |
| twitter:image | https://opengraph.githubassets.com/41f0dd0682ea938f4003c00336648103b0a10469585d7ecd4a13de5d7a6c3536/microsoft/TypeScript/issues/2919 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/41f0dd0682ea938f4003c00336648103b0a10469585d7ecd4a13de5d7a6c3536/microsoft/TypeScript/issues/2919 |
| og:image:alt | Hello TypeScripters. I might be opening a can of worms which might bring an age of darkness upon us (but see PS). Anyway, I've done an attempt to add language support for mixins. For the user, it i... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | dbarbeau |
| hostname | github.com |
| expected-hostname | github.com |
| None | 85bbdfe2b6ba16e234513bc6ceff5d848ef8449820c900692a2b4e67061931e5 |
| 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 | 4943239a6d2c591325dad6e5c313e1cafda877f6 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width