-
Notifications
You must be signed in to change notification settings - Fork 35
Type Parameter Constraints #11
Comments
Thanks for creating this issue. It's already something I'm planning to add and I don't expect it to be too difficult. Internally, the type-checker considers type parameters to have underlying type |
Very cool, sounds like it should fit in nicely with the current implementation then. I'm not sure what you mean by how it would work other kinds of type constraints though. A generic sum type makes me think of something like the following:
|
Here's what I was referring to regarding type constraints and sum types. Suppose you have a generic add function:
What you really would like to do here is constrain What I'm considering is that you could have a (possibly built-in) sum type
Then you can add a type constraint to the
(This brings up a question about how the Maybe this is not worth the complexity and we should just stick to interfaces as the only kind of type constraint. I just thought it was worth mentioning as something I'm considering. |
Ahh, thanks for the clarification, I like the idea! Perhaps another approach would be to support generic interfaces, in which case you could define a generic
|
Hi! I was wondering what your thoughts were on supporting type constraints so that one could require that a type T must implement a certain interface. For example (playground):
This program fails to compile with the following error:
which makes sense since we don't require that the type
T
inDescription
implementsDescriber
so we can't callDescribe
ont
. But we could perhaps constrainT
to require that it implementsDescriber
:Then we could safely call
Describe
because the compiler will enforce that all typesT
implementDescriber
. Supporting such a feature might make it possible to use generics in more places where interfaces are used currently. Anyway, just interested in what your thoughts were. Thanks!P.S. This project is really cool!
The text was updated successfully, but these errors were encountered: