形式的模式匹配:Option {..}<- [英] pattern matching of the form: Option{..} <-

查看:82
本文介绍了形式的模式匹配:Option {..}<-的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这种模式匹配的形式是什么: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
    ...

似乎重新定义了cabalnostylish.模式匹配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:

记录通配符语法允许在记录模式中使用"..",其中每个被忽略的字段f被模式f = f代替.

Record wildcard syntax permits a ".." in a record pattern, where each elided field f is replaced by the pattern f = f.

所以这段代码等同于

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 {..}&lt;-的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆