Access class member types through bound type variables #1463
Unanswered
ImogenBits
asked this question in
Q&A
Replies: 1 comment 1 reply
-
There's a more general idea of being able to refer to the inferred type of a symbol, which is supported by other typing systems, such as TypeScript, so there's precedent for it and it's being discussed here: #769 |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
A common pattern in my code is to have classes that are related like this:
That is, classes that have methods that either take or return some type that is defined by that class. If I want to type this shared interface the best I can do right now is
That works in principle but has two big disadvantages: the first is that all code that uses the common interface needs to have a bunch of type variables everywhere, the other that you can't use the instantiation of T at runtime. The workaround for that is
but that again is somewhat annoying since every implementation essentially specifies T twice, once for the type system and once for the runtime. I feel like a very nice feature would be to be able to access member types from bound type variables (and Self) directly. Then we could just do
From what I can tell this feels like a pretty easy change, the syntax is already there and it's extremely intuitive. You'd only have to touch the runtime attribute access on the TypeVar and Self constructs and make type checkers recognize it. Is there a deeper reason why this isn't a thing or has this just not been worth the effort so far?
Beta Was this translation helpful? Give feedback.
All reactions