How do Haskell field labels work?

I look at this example using getOpts, and one part of it really puzzles me: field label syntax.

First, it seems fairly simple by creating a data type and declaring initial values:

data Options = Options  { optVerbose    :: Bool
                        , optInput      :: IO String
                        , optOutput     :: String -> IO ()
                        }

startOptions :: Options
startOptions =  Options { optVerbose    = False
                        , optInput      = getContents
                        , optOutput     = putStr
                        }

Then it is getOptused to go through the options and determine the actual parameters for the running program using the command foldl... and then this expression expresses disappointment with me:

let Options { optVerbose = verbose
            , optInput = input
            , optOutput = output   } = opts

After that, Boolean and functions are used verbose, inputand output. In most programming languages ​​that I'm familiar with, this step will be written something like this:

verbose = opts.optVerbose
input   = opts.optInput
output  = opts.optOutput

Is Haskell's behavior here somehow documented?

+3
3

- , let (x:xs) = someList x, xs.

, :

let verbose = optVerbose opts
    input   = optInput opts
    output  = optOutput opts

Haskell ML, .

+9

Haskell , / , . ,

data R a = R {v :: a,
              f :: a -> a}

R a, x :: a g :: a -> a,

r = R {v = x, f = g}

:

get_v :: R a -> a
get_v = v

get_f :: R a -> a -> a
get_f = f,

I.e., get_f = f get_v = v. , :

set_v :: R a -> a -> R a
set_v r a = r {v = a}

set_f :: R a -> (a -> a) -> R a
set_f r g = r {f = g}

I.e., set_v set_f

record {field = value}

field - . Haskell , - . , . let Options .

( , , ...)

+5

- (). - , , . , .

optVerbose , optVerbose.

like

optVerbose opts
+1

Source: https://habr.com/ru/post/1765726/


All Articles