Pab*_* C. 7 haskell types xmonad compiler-errors typeclass
我正在配置xmonad,因为我必须启动几个dzen实例,所以我认为最好使用一个函数来获取x和y位置,宽度,高度和文本对齐的参数:
-- mydzen.hs
import Data.List
-- | Return a string that launches dzen with the given configuration.
myDzen :: Num a => a -> a -> a -> Char -> String -> String
myDzen y x w ta e =
intercalate " "
[ "dzen2"
, "-x" , show x
, "-w" , show w
, "-y" , show y
, "-h" , myHeight
, "-fn" , quote myFont
, "-bg" , quote myDBGColor
, "-fg" , quote myFFGColor
, "-ta" , [ta]
, "-e" , quote e
]
quote :: String -> String
quote x = "'" x "'"
-- dummy values
myHeigth = "20"
myFont = "bitstream"
myDBGColor = "#ffffff"
myFFGColor = "#000000"
Could not deduce (Show a) arising from a use of `show'
from the context (Num a)
bound by the type signature for
myDzen :: Num a => a -> a -> a -> Char -> String -> String
at mydzen.hs:(5,1)-(17,13)
Possible fix:
add (Show a) to the context of
the type signature for
myDzen :: Num a => a -> a -> a -> Char -> String -> String
In the expression: show x
In the second argument of `intercalate', namely
`["dzen2", "-x", show x, "-w", ....]'
In the expression:
intercalate " " ["dzen2", "-x", show x, "-w", ....]
Run Code Online (Sandbox Code Playgroud)
显然,删除签名,或更改Num a为Show a解决问题,但我无法理解为什么.在"参数" x,w而y被认为是几乎任何类型的数字(100,550.2,1366 * 0.7ECC).
我是haskell的新手,直到现在我还没能(明确地)理解错误或发现错误.
sdc*_*vvc 17
此前,Show和Eq人的超类Num和代码将编译.
在新的GHC,从7.4版本,这种情况已经改变,现在Num不依赖于Show和Eq,所以你需要将它们添加到签名.
原因是关注点分离 - 有数字类型没有明智的相等和显示功能(可计算的实数,函数环).
并非所有数字都是可显示的(标准类型喜欢Int或者Integer是,但你可以定义自己的类型;编译器如何知道?)你show x在函数中使用 - 因此,a必须属于Show类型类.您可以将签名更改为myDzen :: (Num a, Show a) => a -> a -> a -> Char -> String -> String并删除错误.