Generic extensions? #1206
Replies: 2 comments 7 replies
-
Stupid question: why can't we just do what we do with the LLVM builtins, and wrap those builtin things in other things that have actual declarations in the standard library so they can be extended? |
Beta Was this translation helpful? Give feedback.
-
Since
Bringing up type constructors strikes me as needlessly complicating this discussion. I've never heard anyone speak of doing name lookup in a function (the type constructor in this case) as though the function was a scope, except where we are looking up local variables of the function. But in this case we are certainly not doing that. Why not just say it's performed as QNL in the scope of the unbound (or bound-only-by-universal-quantifiers) generic type?
You're losing me. Playing your example out I get
What's I'm trying to work my way through the rest of what you wrote but I hope you'll clarify this part first. |
Beta Was this translation helpful? Give feedback.
-
We have 4 generic built-in types in Hyo:
Metatype
,Tuple
,Union
, andBuffer
. The compiler currently performs magic to deal with this types, in the sense that an expressionUnion<A, B>
is processed differently than sayMap<A, B>
. That is all fine and well but eventually we'll want to declare extensions and conformances of these generic types, and that creates a problem.We can already create an extension for a concrete specialization of these types. For example:
What we can't do, however, is declare an extension for all buffers. This problem is of course not limited to buffers.
One compounding problem is our implementation of generic parameters. In
hc
, all generic parameters are identified by their declaration. However, built-in types have no declaration. They simply "exist". So even if we could writeextension Metatype { ... }
we'd have no way to "name" the parameter of the type.I found of several approaches to solve this problem and one of them was to allow extensions and conformances to declare generic parameters of their own.
Now we have a way to refer to the generic parameter of
Buffer
, which we use in the where-clause.I think this approach is sound but it's also relatively "expensive" in terms of complexity implementation and language complexity. One issue is that it creates new ways to express things we could already write. For example, we can already write the following extension with a where-clause specifying
Key == Element
:My question is, therefore, whether there are other use cases that could definitely sell this feature. If not, then we can fallback on one of the other solutions I thought of. The motivation for pursuing this one is that it's the least "magic" of all.
Beta Was this translation helpful? Give feedback.
All reactions