我正在尝试理解 Haskell 中的类。我写了几行愚蠢的代码来掌握它。我写了一个名为的类Slang
,它有一个功能。当我将 Integer 作为我的类的实例时,它工作正常。但是当我将 String 作为我的类的实例时,它不会编译。我一直在根据错误输出告诉我的内容对程序坐立不安,但无济于事。我知道它为什么起作用...
这是错误后面的代码:
module Practice where
class Slang s where
slangify :: s -> String
instance Slang Integer where
slangify int = "yo"
instance Slang String where -- When I take this segment out, it works fine
slangify str = "bro"
错误:
Prelude> :load Practice
[1 of 1] Compiling Practice ( Practice.hs, interpreted )
Practice.hs:9:10:
Illegal instance declaration for `Slang String'
(All instance types must be of the form (T t1 ... tn)
where T is not a synonym.
Use -XTypeSynonymInstances if you want to disable this.)
In the instance declaration for `Slang String'
Failed, modules loaded: none.
Prelude>