Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pattern matching of the form: 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
    ...

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.

like image 468
ErikR Avatar asked Aug 22 '13 05:08

ErikR


People also ask

What is pattern matching give an example?

For example, x* matches any number of x characters, [0-9]* matches any number of digits, and . * matches any number of anything. A regular expression pattern match succeeds if the pattern matches anywhere in the value being tested.

What is pattern in pattern matching?

In computer science, pattern matching is the act of checking a given sequence of tokens for the presence of the constituents of some pattern.

What is pattern matching command?

Pattern matching is used by the shell commands such as the ls command, whereas regular expressions are used to search for strings of text in a file by using commands, such as the grep command.


1 Answers

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
like image 57
rkhayrov Avatar answered Sep 27 '22 19:09

rkhayrov