在学习Optics包中的Haskell镜头时,我遇到了以下示例:
data Person = Person
{ _name :: String
, _age :: Int
}
makeLenses ''Person
makePrisms 'PersonName类型的值代表什么?单/双单队列/撇号之间的区别是什么?
两者似乎具有相同的类型:
makeLenses, makePrisms :: Name -> DecsQtemplate-haskell documentation对我来说是无法理解的。它侧重于语法,缺少示例:
* 'f has type Name, and names the function f. Similarly 'C has type Name and names the data constructor C. In general '⟨thing⟩ interprets ⟨thing⟩ in an expression context.
* ''T has type Name, and names the type constructor T. That is, ''⟨thing⟩ interprets ⟨thing⟩ in a type context.发布于 2021-09-26 08:17:06
我们有两种形式的引号来区分数据构造函数和类型构造函数。
考虑这个变种:
data Person = KPerson
{ _name :: String
, _age :: Int
}
makeLenses ''Person -- the type constructor
makePrisms 'KPerson -- the data constructor很明显,在一种情况下,我们为类型构造函数使用Name,而在另一种情况下,我们为数据构造函数使用Name。
原则上,只要Person和KPerson等构造函数的名称始终保持不同,Haskell就可以使用单一形式的引用。由于情况并非如此,我们需要在命名类型和数据构造函数之间消除歧义。
请注意,在实践中,习惯上两个构造函数使用相同的名称,因此在实际代码中经常需要这种消除歧义的方法。
发布于 2021-09-26 08:14:51
类型构造函数和术语构造函数在Haskell中可以具有相同的名称,因此您可以分别使用双勾和单勾来表示不同之处。以下是Optics中具有不同名称示例:
data Person = P
{ _name :: String
, _age :: Int
}
makeLenses ''Person
makePrisms 'Phttps://stackoverflow.com/questions/69332931
复制相似问题