Strong duck typing by generic constraint interface #2504
Replies: 2 comments
-
The CLR is what enforces constraints, not the compiler. Without modifications to the CLR to support this the above would fail even if the compiler allowed it to be compiled. There are proposals to allow constraints based on the members. I believe that proposal and syntax would more accurately represent what you want to achieve. Interfaces are nominal contracts meaning that sharing compatible members isn't sufficient in order to conform to the contract. |
Beta Was this translation helpful? Give feedback.
-
@HaloFour Right I forgot to mention that this require CLR support Constraints based on the members is #9595 which also my proposal. But I think it might easier to reuse interface. So I was try to propose alternative And for completely conform the contract, that's actually the reason I propose #14233. With this and that implement at the same time we could just use interface as trait |
Beta Was this translation helpful? Give feedback.
-
This may related to #9595 but would be easier to implement because this does not introduce new syntax but instead extend the currently worked syntax to support more
Actually it is the same functionality for #9595 that try to make strong static type could polymorph without contract. But this proposal would used interface instead of unknown type
Normally generic constraint would expect that the type of argument must be exactly the type of constraint
But I would like to propose that we could extend this part of compiler to let generic function could accept any type. And only check that the type we input has the same member as the type we constraint
Beta Was this translation helpful? Give feedback.
All reactions