Tôi đang chơi xung quanh cố gắng hiểu các lớp học trong Haskell. Tôi đã viết một vài dòng mã ngớ ngẩn để thu thập nó. Tôi đã viết một lớp có tên là Slang
có một chức năng. Khi tôi làm cho Integer trở thành một thể hiện của lớp, nó hoạt động tốt. Nhưng khi tôi tạo String một thể hiện của lớp của tôi, nó sẽ không biên dịch. Tôi đã được fidgeting với chương trình dựa trên những gì đầu ra lỗi cho tôi biết nhưng vô ích. Tôi có ý tưởng tại sao nó hoạt động ...Không thể tạo chuỗi một thể hiện của một lớp trong Haskell
Đây là đoạn mã tiếp theo là lỗi:
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"
LỖI:
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>
Có bạn đã đúng !! Xin lỗi, tôi đã sửa nó – CodyBugstein