Title: Extendable typed functions · Issue #447 · dry-python/returns · GitHub
Open Graph Title: Extendable typed functions · Issue #447 · dry-python/returns
X Title: Extendable typed functions · Issue #447 · dry-python/returns
Description: Ok, here's the deal. We need to somehow make our pointfree and other helper functions trully generic. Currently we have a hardcoded set of containers that each of these functions support. It is not a scallable solution. Let's say I want ...
Open Graph Description: Ok, here's the deal. We need to somehow make our pointfree and other helper functions trully generic. Currently we have a hardcoded set of containers that each of these functions support. It is not...
X Description: Ok, here's the deal. We need to somehow make our pointfree and other helper functions trully generic. Currently we have a hardcoded set of containers that each of these functions support. It is...
Opengraph URL: https://github.com/dry-python/returns/issues/447
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Extendable typed functions","articleBody":"Ok, here's the deal.\r\n\r\nWe need to somehow make our pointfree and other helper functions trully generic.\r\nCurrently we have a hardcoded set of containers that each of these functions support.\r\n\r\nIt is not a scallable solution. \r\n\r\nLet's say I want to create a new monad called `Example[T] extends Monad[T]`.\r\n\r\nHere's what I need to do:\r\n\r\n```python\r\nfrom typing import Callable, Generic, TypeVar\r\nfrom returns.typeclasses.monad import Monad\r\nfrom returns.hkt import Kind\r\n\r\nT = TypeVar('T')\r\nN = TypeVar('N')\r\n\r\nclass Example(Kind['Example', T], Generic[T], Monad[T]):\r\n def bind(self, function: Callable[[T], Kind['Example', N]]) -> 'Example[N]':\r\n ...\r\n```\r\n\r\nIt works just fine! Now, what about using `bind()` pointfree function? It will work in runtime, but fail during the type-checking.\r\nBecause it is defined using `overload`s and is hardcoded.\r\n\r\nThe problem is that HKT functions are not supported. And possibly won't be supported anytime soon (unless `get_function_sig_hook` will be released).\r\n\r\nI suggest to rewrite it as follows (let's start with the contract definition):\r\n\r\n```python\r\nK = TypeVar(K, bound=Monad)\r\n\r\n@contract\r\ndef bind(instance: Monad[T], function: Callable[[T], Monad[N]]) -> Monad[N]:\r\n return instance.bind(function)\r\n```\r\n\r\nAnd then add a special case for our existing containers:\r\n\r\n```python\r\nfrom returns.pointfree import bind\r\n\r\n@bind.negotiate\r\ndef bind_maybe(instance: Maybe[T], function: Callable[[T], Maybe[N]]) -> Maybe[N]:\r\n \"\"\"Case for Maybe.\"\"\"\r\n\r\n@bind.negotiate\r\ndef bind_example(instance: Example[T], function: Callable[[T], Example[N]]) -> Example[N]:\r\n \"\"\"Case for Example.\"\"\"\r\n```\r\n\r\nThis way we would provide:\r\n1. Default implementation for types we own\r\n2. Ability to add your own types and extend the typing of existing things","author":{"url":"https://github.com/sobolevn","@type":"Person","name":"sobolevn"},"datePublished":"2020-07-05T16:22:13Z","url":"https://github.com/dry-python/returns/issues/447"}
| 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:16ba16be-543c-6773-06b3-cd528dbf296e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C7CE:3E4AC7:28CC76D:376E7C2:6B004571 |
| html-safe-nonce | 0b43d8f1ab4e74c32a6caee60877b4646f15fb4cfa13875a74a75df011fb8c33 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDN0NFOjNFNEFDNzoyOENDNzZEOjM3NkU3QzI6NkIwMDQ1NzEiLCJ2aXNpdG9yX2lkIjoiNDYzMTgwNDU0ODk4MDE2Mzk1MyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 56f375b70addd3fb6ed5519a4f4436d4af50bc55e6c752211b6393843cbd0f24 |
| hovercard-subject-tag | issue:651100051 |
| 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/dry-python/returns/447/issue_layout |
| twitter:image | https://opengraph.githubassets.com/502cef1b2ebcbfde68684819c2401e44f11f5ceca509b968c4cf6a41f3e56c18/dry-python/returns/issues/447 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/502cef1b2ebcbfde68684819c2401e44f11f5ceca509b968c4cf6a41f3e56c18/dry-python/returns/issues/447 |
| og:image:alt | Ok, here's the deal. We need to somehow make our pointfree and other helper functions trully generic. Currently we have a hardcoded set of containers that each of these functions support. It is not... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | sobolevn |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7d0829bbde9e48b2486cb5dd6d147896eb01d32134627091f818958a935ef3f0 |
| turbo-cache-control | no-preview |
| go-import | github.com/dry-python/returns git https://github.com/dry-python/returns.git |
| octolytics-dimension-user_id | 37993755 |
| octolytics-dimension-user_login | dry-python |
| octolytics-dimension-repository_id | 167689891 |
| octolytics-dimension-repository_nwo | dry-python/returns |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 167689891 |
| octolytics-dimension-repository_network_root_nwo | dry-python/returns |
| 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 | f3f3a43ca095cdea3abbb394112e971f9fdb94fa |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width