data Foo =
FooString String
…
class Fooable a where --(is this a good way to name this?)
toFoo :: a -> Foo
我要讓String
的Fooable
一個實例:
instance Fooable String where
toFoo = FooString
GHC則抱怨:
Illegal instance declaration for `Fooable 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 `Fooable String'
相反,如果我使用[Char]
:
instance Fooable [Char] where
toFoo = FooString
GHC抱怨:
Illegal instance declaration for `Fooable [Char]'
(All instance types must be of the form (T a1 ... an)
where a1 ... an are type *variables*,
and each type variable appears at most once in the instance head.
Use -XFlexibleInstances if you want to disable this.)
In the instance declaration for `Fooable [Char]'
問題:
- 我爲什麼不能做字符串和類型類的實例?
- 如果我添加一個額外的標誌,GHC似乎願意讓我逃脫。這是一個好主意嗎?
這是我給予好評的那種問題,並標記爲收藏,否則我知道的不久的將來,我會問它;) – 2011-05-09 19:52:04
關於額外的標誌:這可能是一個好主意,只要你信任GHC並理解標誌的作用。想起[Yesod](http://www.yesodweb.com/):它鼓勵您在編寫Yesod應用程序時始終使用OverloadedStrings編譯指示,而QuasiQuotes是Yesod路由規則的必要條件。請注意,在編譯時不要使用標誌,您還可以在.hs文件的頂部放置'{ - #LANGUAGE FlexibleInstances# - }'(或任何其他編譯指示)。 – 2011-05-10 01:39:22