【发布时间】:2016-10-04 09:16:16
【问题描述】:
我熟悉 newtype 声明:
newtype MyAge = Age {age :: Int} deriving (Show, Eq, Ord)
在这种情况下,Age 是一个 Int,但是我遇到了下面的代码,但我无法理解:
newtype Ages a = Ages {age :: String -> [(a,String)]}
这似乎是一个函数声明? (接受字符串,返回包含 'a' 和字符串的元组列表)- 这是正确的吗?
注意,我刚刚意识到这只是声明函数的基本记录语法。
另外,我已经尝试实现这种类型,但我一定做错了什么:
newtype Example a = Example {ex :: Int -> Int}
myexample = Example {ex = (\x -> x + 1)}
这可以编译,但是我不明白为什么我没有传递 'a' 参数?
【问题讨论】:
-
不要向现有帖子添加新问题。也就是说,
Example a是使用 phantom type 的一个示例。