What does the "where" do in interface Addable<T> where T : Addable<T> {} #1348
-
The following code seems to work a "where" in the Animal interface declaration. I understand why Mammal needs the "where" (otherwise the compiler can't resolve EatLunch). It seems that louthy puts the "where" on all interfaces. Just wondering if there's additional information I don't understand about the compiler that makes it a good idea to always add the "where".
UPDATE:
Looking at some code, "all" is an exaggeration. But for example the following compiles completely fine without the "where" on the Mappable interface.
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
I cover this in-depth in my higher-kinds series: https://paullouth.com/higher-kinds-in-c-with-language-ext/ See the section on 'traits' |
Beta Was this translation helpful? Give feedback.
Put this into a console project and try removing the constraint:
You'll see that the virtual method doesn't compile any more.
By enforcing the implementer of
Monoid<A>
to be theA
itself we makestatic
default behaviours available. Also, it's important that something likeOption
actually implements the behaviours ofFunctor<…