You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As you can see, both monster and knight have similar characteristics, but they
also have some differences. So it is possible to describe their common
properties using typeclasses, but they are different data types in the end.
Do typeclasses describe their common properties (e.g. health) or the common actions that can be taken on them (e.g. fight)?
The text was updated successfully, but these errors were encountered:
In Haskell, a typeclass for values that have Health:
classHasHealthawheregetHealth::a->Health
and typeclass, describing the fight between two values of the same type:
classFighterawherefight::a->a->FightResult
However, as you can see, this Fighter typeclass allows to fight only values of the same type and not others. If you design the Fighter typeclass differently:
classFighterawhere... something clever here ...
You can implement a top-level function (not a typeclass method) that performs fight between two values of different types:
Chapter3 says:
Do typeclasses describe their common properties (e.g. health) or the common actions that can be taken on them (e.g. fight)?
The text was updated successfully, but these errors were encountered: