简体   繁体   English

这是什么意思?

[英]What does (..) mean?

I'm trying to learn Haskell. 我正在努力学习Haskell。

I'm reading the code on here[1]. 我在这里阅读代码[1]。 I just copy and past some part of the code from lines:46 and 298-300. 我只是复制并过去代码的一部分:46和298-300。

Question : What does (..) mean? 问题 :(..)是什么意思?

I Hoggled it but I got no result. 我很难过,但我没有结果。

module Pos.Core.Types(
-- something here

 SharedSeed (..) -- what does this mean?

) where


newtype SharedSeed = SharedSeed
{ getSharedSeed :: ByteString
} deriving (Show, Eq, Ord, Generic, NFData, Typeable)

[1] https://github.com/input-output-hk/cardano-sl/blob/master/core/Pos/Core/Types.hs [1] https://github.com/input-output-hk/cardano-sl/blob/master/core/Pos/Core/Types.hs

It means "export all constructors and record fields for this data type". 它表示“导出此数据类型的所有构造函数和记录字段”。

When writing a module export list, there's three 4 ways you can export a data type: 当编写一个模块的出口列表中,有3点4的方式,你可以导出的数据类型:

module ModuleD(
    D,       -- just the type, no constructors
    D(..),   -- the type and all its constructors
    D(DA)    -- the type and the specific constructor
    ) where

data D = DA A | DB B

If you don't export any constructors, the type, well, can't be constructed, at least directly. 如果不导出任何构造函数,则无法构造类型,至少直接构造。 This is useful if you eg want to enforce some runtime invariants on the data type: 如果您想要对数据类型强制执行某些运行时不变量,这将非常有用:

module Even (evenInt, toInt) where

newtype EvenInt = EvenInt Int deriving (Show, Eq)

evenInt :: Int -> Maybe EvenInt
evenInt x = if x `mod` 2 == 0 then Just x else Nothing

toInt :: EvenInt -> Int
toInt (EvenInt x) = x

The caller code can now use this type, but only in the allowed manner: 调用者代码现在可以使用此类型,但只能以允许的方式使用:

x = evenInt 2
putStrLn $ if isJust x then show . toInt . fromJust $ x else "Not even!"

As a side note, toInt is usually implemented indirectly via the record syntax for convenience: 作为旁注,为方便起见, toInt通常通过记录语法间接实现:

data EvenInt = EvenInt { toInt :: Int }

4 See @leftaroundabout 's answer 4@leftaroundabout的回答

The syntax of import/export lists has not much to do with the syntax of Haskell itself. 导入/导出列表的语法与Haskell本身的语法没有多大关系。 It's just a comma-separated listing of everything you want to export from your module. 它只是一个以逗号分隔的列表,列出了您要从模块中导出的所有内容。 Now, there's a problem there because Haskell really has two languages with symbols that may have the same name. 现在,那里存在一个问题,因为Haskell确实有两种语言,其符号可能具有相同的名称。 This is particularly common with newtype s like the one in your example: you have a type-level name SharedSeed :: * , and also a value-level name (data constructor) SharedSeed :: ByteString -> SharedSeed . 这在newtype特别常见,例如你的例子:你有一个类型级名称 SharedSeed :: * ,还有一个值级名称(数据构造函数) SharedSeed :: ByteString -> SharedSeed

This only happens with uppercase names, because lowercase at type level are always local type variables. 这只发生在大写名称中,因为类型级别的小写始终是本地类型变量。 Thus the convention in export lists that uppercase names refer to types . 因此,导出列表中的约定是大写名称引用类型

But just exporting the type does not allow users to actually construct values of that type . 但只是导出类型不允许用户实际构造该类型的值 That's often prudent: not all internal-representation values might make legal values of the newtype (see Bartek's example ), so then it's better to only export a safe smart constructor instead of the unsafe data constructor. 这通常是谨慎的:并非所有的内部表示值都可能构成newtype的合法值(参见Bartek的例子 ),因此最好只导出一个安全的智能构造函数而不是不安全的数据构造函数。

But other times, you do want to make the data constructor available, certainly for multi-constructor types like Maybe . 但有时候,你确实想要使数据构造函数可用,当然对于像Maybe这样的多构造函数类型。 To that end, export lists have three syntaxes you can use: 为此,导出列表有三种语法可供您使用:

module Pos.Core.Types(
  • SharedSeed (SharedSeed) will export the constructor SharedSeed . SharedSeed (SharedSeed)将导出构造函数SharedSeed In this case that's of course the only constructor anyway, but if there were other constructors they would not be exported with this syntax. 在这种情况下,这当然是唯一的构造函数,但如果有其他构造函数,则不会使用此语法导出它们。

  • SharedSeed (..) will export all constructors. SharedSeed (..)将导出所有构造函数。 Again, in this case that's only SharedSeed , but for eg Maybe it would export both Nothing and Just . 同样,在这种情况下,只有这SharedSeed ,但如Maybe也将出口都NothingJust

  • pattern SharedSeed will export ShareSeed as a standalone pattern (independent of the export of the ShareSeed type). pattern SharedSeedShareSeed导出为独立模式 (独立于ShareSeed类型的导出)。 This requires the -XPatternSynonyms extension . 这需要-XPatternSynonyms扩展名

     ) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM