问题描述
这种模式匹配的形式是什么:Option{..} <- ...
,例如如此处所用:
What is this form of pattern matching called:Option{..} <- ...
, e.g. as it is used here:
data Option = Option { cabal :: Maybe String , noStylish :: Bool }
...
main = do
Option{..} <- cmdArgs defOption
cabp <- case cabal of
Nothing -> do
...
似乎重新定义了cabal
和nostylish
.模式匹配cabal
之前的类型为Option -> Maybe String
,但是之后为Maybe String
.
It seems to redefine cabal
and nostylish
. Before the pattern match cabal
has type Option -> Maybe String
but after it has type Maybe String
.
此示例来自最近上传的软件包cabal2ghci
.
This example comes from the recently uploaded package cabal2ghci
.
推荐答案
这是GHC语法扩展,称为.报价文件:
This is a GHC syntactic extension called record wildcards. Quoting documentation:
所以这段代码等同于
Option { cabal = cabal, noStylish = noStylish } <- cmdArgs defOption
为记录类型中的每个字段有效地将名称x
绑定到名为x
的记录字段的值.
effectively binding name x
to the value of record field named x
for every field in the record type.
<-
部分与此处无关,您也可以编写
<-
part is irrelevant here, you can as well write
let Option { .. } = some expression
这篇关于形式的模式匹配:Option {..}<-的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!