【发布时间】:2019-12-10 22:57:39
【问题描述】:
我正在学习 Haskell 中的幻像类型。我知道幻像类型是参数化类型,其参数不会出现在其定义的右侧。
但是,我想知道使用newtype 声明的所有类型是否都是幻像类型。
当我尝试使用以下代码编译程序时:
newtype SpecialInt Int = Special Int
我收到一条错误消息:
Unexpected type ‘Int’
In the newtype declaration for ‘SpecialInt’
A newtype declaration should have form
newtype SpecialInt a = ...
如果左侧的类型参数没有出现在右侧,这让我相信新类型是幻像类型。 例如。
newtype SpecialInt a = Special a
不会是幻影类型,但是
newtype SpecialInt a = Special Int
将是幻像类型。
因此,并非所有使用newtype 声明的类型都是幻像类型。但是,我不确定我的推理是否正确。
【问题讨论】:
-
你可以写
newtype SpecialInt = Special Int。
标签: haskell types functional-programming