【发布时间】:2016-09-22 23:06:35
【问题描述】:
我是 Haskell 的新手,我四处寻找以下问题的答案,但没有运气。
为什么这段代码不能编译?
newtype Name = Name String deriving (Show, Read)
newtype Age = Age Int deriving (Show, Read)
newtype Height = Height Int deriving (Show, Read)
data User = Person Name Age Height deriving (Show, Read)
data Characteristics a b c = Characteristics a b c
exampleFunction :: Characteristics a b c -> User
exampleFunction (Characteristics a b c) = (Person (Name a) (Age b) (Height c))
错误:
"Couldn't match expected type ‘String’ with actual type ‘a’,‘a’ is a rigid type, variable bound by the type signature"
但是,这编译得很好:
exampleFunction :: String -> Int -> Int -> User
exampleFunction a b c = (Person (Name a) (Age b) (Height c))
我意识到有更简单的方法可以完成上述操作,但我只是在测试自定义数据类型的不同用途。
更新:
我的倾向是编译器不喜欢'exampleFunction ::Characteristics a b c',因为它不是类型安全的。即我不保证:a == Name String, b == Age Int, c == Height Int.
【问题讨论】: