Biorąc:Dlaczego nie mogę uczynić String instancją egzemplarza typograficznego?
data Foo =
FooString String
…
class Fooable a where --(is this a good way to name this?)
toFoo :: a -> Foo
Chcę, aby String
wystąpienie Fooable
:
instance Fooable String where
toFoo = FooString
GHC potem narzeka:
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'
Gdybym zamiast używać [Char]
:
instance Fooable [Char] where
toFoo = FooString
GHC narzeka:
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]'
Pytanie:
- Dlaczego nie mogę zrobić String i wystąpienie typeclass?
- GHC wydaje się skłonny pozwolić mi uciec, jeśli dodam dodatkową flagę. Czy to dobry pomysł?
Jest to rodzaj pytań, upvote i znak jako ulubione, ponieważ w przeciwnym razie wiem, w sposób w niedalekiej przyszłości będę o to pytać;) –
Odnośnie dodatkowej flagi: to chyba dobry pomysł, o ile ufasz GHC i rozumiesz, co robi flaga. [Yesod] (http://www.yesodweb.com/) przychodzi na myśl: zachęca do korzystania zawsze z pragmy OverloadedStrings podczas pisania aplikacji Yesod, a QuasiQuotes to konieczność dla reguł routingu Yesod. Zauważ, że zamiast flagi podczas kompilacji możesz także umieścić '{- # LANGUAGE FlexibleInstances # -}' (lub dowolną inną pragmę) na górze pliku .hs. –