我有一个类HasFavorite
,它为某些域分配了我相应的收藏夹。
class HasFavorite domain where
favorite :: domain
data Animal = Monkey | Donkey
deriving Show
data Color = Red | Blue
deriving Show
instance HasFavorite Animal where
favorite = Donkey
instance HasFavorite Color where
favorite = Blue
现在,我想定义这个:
rant :: (Show d, HasFavorite d) => d -> String
rant x = (show x) ++ "sucks. " ++ (show (favorite :: d)) ++ " is better."
对于每次调用,rant x
都有一个favorite :: d
可以解析的具体类型。为什么 Haskell 不能这样做?
我试过添加{-# LANGUAGE ScopedTypeVariables #-}
,但没有帮助。