3 polymorphism haskell types typeclass
我试图定义一个代数类型:
data MyType t = MyType t
Run Code Online (Sandbox Code Playgroud)
并使它成为Show的一个实例:
instance Show (MyType t) where
show (MyType x) = "MyType: " ++ (show x)
Run Code Online (Sandbox Code Playgroud)
GHC抱怨因为它不能推断'Show(MyType t)'中的类型't'实际上是Show的实例,这是(show x)所需要的.
我不知道在哪里以及如何声明't'是Show的一个实例?
ear*_*arl 19
在类型上添加类型约束t:
instance Show t => Show (MyType t) where
show (MyType x) = "MyType: " ++ (show x)
Run Code Online (Sandbox Code Playgroud)
Don*_*art 15
你也可以:
data MyType t = MyType t
deriving Show
Run Code Online (Sandbox Code Playgroud)
如果你想要一个常规的节目格式.