René's URL Explorer Experiment


Title: [RFC] New Generics · Issue #679 · wurstscript/WurstScript · GitHub

Open Graph Title: [RFC] New Generics · Issue #679 · wurstscript/WurstScript

X Title: [RFC] New Generics · Issue #679 · wurstscript/WurstScript

Description: Currently, generics are translated using erasure. This means, that generics can only be instantiated with types compatible with int. The idea of this proposal is to change the translation and instead of using erasure translation would ge...

Open Graph Description: Currently, generics are translated using erasure. This means, that generics can only be instantiated with types compatible with int. The idea of this proposal is to change the translation and inste...

X Description: Currently, generics are translated using erasure. This means, that generics can only be instantiated with types compatible with int. The idea of this proposal is to change the translation and inste...

Opengraph URL: https://github.com/wurstscript/WurstScript/issues/679

X: @github

direct link

Domain: patch-diff.githubusercontent.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[RFC] New Generics","articleBody":"Currently, generics are translated using erasure. This means, that generics can only be instantiated with types compatible with `int`. The idea of this proposal is to change the translation and instead of using erasure translation would generate one copy per instantiated type. This would allow to extend generics with some interesting new features, but would also be incompatible in some cases.\r\n\r\nLanguage changes:\r\n============\r\n\r\n## 1. Allow all types for generics\r\n\r\n\r\nEvery type can be inserted for a generic type parameter without any restrictions.\r\n\r\nFor example, we can then use `LinkedList\u003cvec3\u003e`.\r\n\r\nThis also allows us to get rid of the implicit calls to `fromIndex` and `toIndex` that we currently have as a woraround.\r\n\r\n## 2. Allow to restrict type parameters / type classes\r\n\r\nSometimes we want to restrict type parameters.\r\nFor example `LinkedList\u003cT\u003e.toString() returns string` only can be implemented, if we have a method `toString(T) returns string`.\r\n\r\nWe could of course provide this method using an additional argument:  \r\n\r\n```ceylon\r\nLinkedList\u003cT\u003e.toString(Show\u003cT\u003e elementShower) returns string\r\n      ... elementShower.toString(x) ...\r\n\r\ninterface Show\u003cT\u003e\r\n    function toString(T elem) returns string\r\n\r\n```\r\n\r\nHowever, it is not very convenient to always pass this `Show`value explicitly. \r\n\r\nInstead we allow to use generic interfaces as type constraints:\r\n\r\n```ceylon\r\nfunction LinkedList\u003cT\u003e.toString\u003cT : Show\u003e() returns string\r\n      ... T.toString(x) ...\r\n```\r\n\r\n```ceylon\r\ninterface Serializable\u003cT\u003e\r\n    function encode(T t) returns string\r\n    function decode(string s) returns T\r\n\r\nfunction LinkedList\u003cT\u003e.encode\u003cT : Serializable\u003e() returns string\r\n    ...\r\nfunction LinkedList\u003cT\u003e.decode\u003cT : Serializable\u003e(string s) returns T\r\n    ...\r\n```\r\n\r\nThe compiler automatically finds instances that are defined with the new `instance` definitions:\r\n\r\n\r\n```ceylon\r\ninstance Show\u003cvec2\u003e\r\n    function toString(vec2 v) returns string\r\n        return \"(\" + v.x.toString() + \", \" + v.y.toString() + \")\"\r\n\r\n// make all lists with serializable elements serializable:\r\ninstance \u003cT : Serializable\u003e implements Serializable\u003cLinkedList\u003cT\u003e\u003e\r\n    // implement methods here\r\n```\r\n\r\nIf multiple instance are in scope there is a compilation error.\r\n\r\nThe instantiation is part of the type, so the following example is not allowed:\r\n\r\n```ceylon\r\npackage A\r\n\r\npublic interface Ord\u003cT\u003e\r\n    function lessEq(T a, T b) returns boolean\r\n\r\ninstance Ord\u003cstring\u003e\r\n    function lessEq(string a, string b) returns boolean\r\n        return a.length() \u003c= b.length()\r\n\r\npublic class TreeSet\u003cT : Ord\u003e\r\n    ...\r\n\r\npublic TreeSet\u003cstring\u003e mySet = TreeSet.create(\"a\",\"aa\",\"aaa\")\r\n\r\npackage B\r\nimport A\r\n\r\ninstance Ord\u003cstring\u003e\r\n    function lessEq(string a, string b) returns boolean\r\n        return a.length() \u003e= b.length()\r\n\r\ninit\r\n    TreeSet\u003cstring\u003e s = mySet // error, because Ord intances are not compatible\r\n\r\n```\r\n\r\nTo resolve instances, we first match the parameters and then match the type constraints from left to right.\r\n\r\n**Type class translation**: The translation uses monomorphization (like traits in Rust) when translating to Jass and dictionary passing when translating to Lua. \r\n\r\n\r\n\r\n## 3. Disallow casting to int\r\n\r\nSince we now allow all types to be used for type parameters, we can no longer cast type parameters to `int`. However, we can use the new feature of restricted type parameters to implement the same feature:\r\n\r\nOld `HashMap`:\r\n\r\n```ceylon\r\npublic class HashMap\u003cK,V\u003e extends Table\r\n\r\n\t/** Whether a value exists under the given key or not */\r\n\tfunction has(K key) returns boolean\r\n\t\treturn hasInt(key castTo int)\r\n\r\n\t/** Saves the given value under the given key */\r\n\tfunction put(K key, V value)\r\n\t\tsaveInt(key castTo int, value castTo int)\r\n\r\n\t/** Retrieves the value saved under the given key */\r\n\tfunction get(K key) returns V\r\n\t\treturn loadInt(key castTo int) castTo V\r\n\r\n\t/** Removes the value saved under the given key */\r\n\tfunction remove(K key)\r\n\t\tremoveInt(key castTo int)\r\n```\r\n\r\nNew `HashMap`\r\n\r\n\r\n\r\n```ceylon\r\ntypeclass Indexable\u003cT\u003e\r\n    function toIndex(T elem) returns int\r\n    function fromIndex(int index) returns T\r\n\r\npublic class HashMap\u003cK : Indexable, V : Indexable\u003e extends Table\r\n\r\n\t/** Whether a value exists under the given key or not */\r\n\tfunction has(K key) returns boolean\r\n\t\treturn hasInt(K.toIndex(key))\r\n\r\n\t/** Saves the given value under the given key */\r\n\tfunction put(K key, V value)\r\n\t\tsaveInt(K.toIndex(key), V.toIndex(value))\r\n\r\n\t/** Retrieves the value saved under the given key */\r\n\tfunction get(K key) returns V\r\n\t\treturn V.fromIndex(loadInt(K.toIndex(key)))\r\n\r\n\t/** Removes the value saved under the given key */\r\n\tfunction remove(K key)\r\n\t\tremoveInt(K.toIndex(key))\r\n```\r\n\r\nTo simplify the transition, we could automatically interpret the old code as the new one.\r\n\r\nAlso we can automatically create some useful instances for all classes, for example the Indexable above.\r\n\r\nA challenge here is that for Jass and Lua different type classes would make sense: \r\nCasting classes to int in Lua is an ugly and leaky workaround and not really needed as we have tables to store stuff without indexes.\r\n\r\n\r\n## 4. Disallowing casting between different instantiations\r\n\r\nCurrently the following code is valid:\r\n\r\n```ceylon\r\nclass A\r\nclass B extends A\r\n\r\nfunction foo(LinkedList\u003cA\u003e l)\r\n\r\n@Test function testCast()\r\n\tLinkedList\u003cB\u003e list = asList(new B, new B, new B)\r\n\tfoo(list castTo int castTo LinkedList\u003cA\u003e)\r\n```\r\n\r\nWith the proposed changes this would still compile, but might no longer work, since translation might create complete different code for `List\u003cA\u003e` and `List\u003cB\u003e`. \r\nMaybe it makes sense to guarantee that all instantiations with class types share the same code (same as it is now), but this is difficult to implement in combination with typeclasses.\r\n\r\n\r\n","author":{"url":"https://github.com/peq","@type":"Person","name":"peq"},"datePublished":"2018-06-21T19:55:30.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":10},"url":"https://github.com/679/WurstScript/issues/679"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:4d694307-8e70-3175-a5a0-2a05c902c10d
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id8A80:3556CB:6787058:8AEEB72:698CB038
html-safe-nonce4f750d3b8bdbaaa1d09ae667b5d5efcd74d07ee8cc10fab9897dfdfe97614fbe
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4QTgwOjM1NTZDQjo2Nzg3MDU4OjhBRUVCNzI6Njk4Q0IwMzgiLCJ2aXNpdG9yX2lkIjoiNjA0MTEzOTI2Nzc5MzUxMDk2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmac0ed17e1e1ada56bfb17c36ce47d4f3b92d8fa41f63773e979cb34d7502e1499e
hovercard-subject-tagissue:334634752
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/wurstscript/WurstScript/679/issue_layout
twitter:imagehttps://opengraph.githubassets.com/34e14a24db723e9d0a4a6789933a4e7f9627a3e567f3183cc1ef37647fd91fb8/wurstscript/WurstScript/issues/679
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/34e14a24db723e9d0a4a6789933a4e7f9627a3e567f3183cc1ef37647fd91fb8/wurstscript/WurstScript/issues/679
og:image:altCurrently, generics are translated using erasure. This means, that generics can only be instantiated with types compatible with int. The idea of this proposal is to change the translation and inste...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamepeq
hostnamegithub.com
expected-hostnamegithub.com
None640eeb7b6ff4d8d106235d228c0c286e82592d4d2403227b5b2b4fc5832297a4
turbo-cache-controlno-preview
go-importgithub.com/wurstscript/WurstScript git https://github.com/wurstscript/WurstScript.git
octolytics-dimension-user_id30814797
octolytics-dimension-user_loginwurstscript
octolytics-dimension-repository_id3584229
octolytics-dimension-repository_nwowurstscript/WurstScript
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id3584229
octolytics-dimension-repository_network_root_nwowurstscript/WurstScript
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release3d444f0a47beeeac94cddbb51c91ab408befe8d4
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://patch-diff.githubusercontent.com/wurstscript/WurstScript/issues/679#start-of-content
https://patch-diff.githubusercontent.com/
Sign in https://patch-diff.githubusercontent.com/login?return_to=https%3A%2F%2Fgithub.com%2Fwurstscript%2FWurstScript%2Fissues%2F679
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub SparkBuild and deploy intelligent appshttps://github.com/features/spark
GitHub ModelsManage and compare promptshttps://github.com/features/models
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
GitHub SponsorsFund open source developershttps://github.com/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/accelerator
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://patch-diff.githubusercontent.com/login?return_to=https%3A%2F%2Fgithub.com%2Fwurstscript%2FWurstScript%2Fissues%2F679
Sign up https://patch-diff.githubusercontent.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=wurstscript%2FWurstScript
Reloadhttps://patch-diff.githubusercontent.com/wurstscript/WurstScript/issues/679
Reloadhttps://patch-diff.githubusercontent.com/wurstscript/WurstScript/issues/679
Reloadhttps://patch-diff.githubusercontent.com/wurstscript/WurstScript/issues/679
wurstscript https://patch-diff.githubusercontent.com/wurstscript
WurstScripthttps://patch-diff.githubusercontent.com/wurstscript/WurstScript
Notifications https://patch-diff.githubusercontent.com/login?return_to=%2Fwurstscript%2FWurstScript
Fork 28 https://patch-diff.githubusercontent.com/login?return_to=%2Fwurstscript%2FWurstScript
Star 236 https://patch-diff.githubusercontent.com/login?return_to=%2Fwurstscript%2FWurstScript
Code https://patch-diff.githubusercontent.com/wurstscript/WurstScript
Issues 77 https://patch-diff.githubusercontent.com/wurstscript/WurstScript/issues
Pull requests 5 https://patch-diff.githubusercontent.com/wurstscript/WurstScript/pulls
Discussions https://patch-diff.githubusercontent.com/wurstscript/WurstScript/discussions
Actions https://patch-diff.githubusercontent.com/wurstscript/WurstScript/actions
Security 0 https://patch-diff.githubusercontent.com/wurstscript/WurstScript/security
Insights https://patch-diff.githubusercontent.com/wurstscript/WurstScript/pulse
Code https://patch-diff.githubusercontent.com/wurstscript/WurstScript
Issues https://patch-diff.githubusercontent.com/wurstscript/WurstScript/issues
Pull requests https://patch-diff.githubusercontent.com/wurstscript/WurstScript/pulls
Discussions https://patch-diff.githubusercontent.com/wurstscript/WurstScript/discussions
Actions https://patch-diff.githubusercontent.com/wurstscript/WurstScript/actions
Security https://patch-diff.githubusercontent.com/wurstscript/WurstScript/security
Insights https://patch-diff.githubusercontent.com/wurstscript/WurstScript/pulse
New issuehttps://patch-diff.githubusercontent.com/login?return_to=https://github.com/wurstscript/WurstScript/issues/679
New issuehttps://patch-diff.githubusercontent.com/login?return_to=https://github.com/wurstscript/WurstScript/issues/679
[RFC] New Genericshttps://patch-diff.githubusercontent.com/wurstscript/WurstScript/issues/679#top
enhancementhttps://github.com/wurstscript/WurstScript/issues?q=state%3Aopen%20label%3A%22enhancement%22
genericshttps://github.com/wurstscript/WurstScript/issues?q=state%3Aopen%20label%3A%22generics%22
unclear/discussionhttps://github.com/wurstscript/WurstScript/issues?q=state%3Aopen%20label%3A%22unclear%2Fdiscussion%22
https://github.com/peq
https://github.com/peq
peqhttps://github.com/peq
on Jun 21, 2018https://github.com/wurstscript/WurstScript/issues/679#issue-334634752
enhancementhttps://github.com/wurstscript/WurstScript/issues?q=state%3Aopen%20label%3A%22enhancement%22
genericshttps://github.com/wurstscript/WurstScript/issues?q=state%3Aopen%20label%3A%22generics%22
unclear/discussionhttps://github.com/wurstscript/WurstScript/issues?q=state%3Aopen%20label%3A%22unclear%2Fdiscussion%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.