pattern matching of the form: Option{..} <-

狂风中的少年 提交于 2019-12-23 07:21:26

问题


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
    ...

It seems to redefine cabal and nostylish. Before the pattern match cabal has type Option -> Maybe String but after it has type Maybe String.

This example comes from the recently uploaded package cabal2ghci.


回答1:


This is a GHC syntactic extension called record wildcards. Quoting documentation:

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

So this code is equivalent to

Option { cabal = cabal, noStylish = noStylish } <- cmdArgs defOption

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


来源:https://stackoverflow.com/questions/18372188/pattern-matching-of-the-form-option

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!