Introduction
A type class defines some types related by their operations. This is saying that typeclasses are usually defined in terms of those operations, and these operations group those type together.
For example, we can put all types that can be converted to a String
in the same type class called Show
.
We can introduce this Show
type class by:
class Show a where
show :: a -> String
Then we can give an instance to the type class.
instance Show String where
show s = s
instance Show Boolean where
show true = "true"
show false = "false"
当前内容版权归 hamler-lang 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 hamler-lang .