I understand how to use record syntax in Haskell, but I have trouble to understand what the thing inside the curly braces is.
data PairRecord = PR {foo::Int, bar::String} deriving (Eq,Show)
x = (PR {foo=1, bar="init"})
y= x {foo=23}
{foo=23}
thing? The last line looks as if it was an
argument to the function x, which is clearly not the case. {foo=23}
except placing it right behind
a record? do
notation?This syntax is called "record update" or "update using field labels" and described in Section 3.15.3 of the Haskell 2010 report. It can be desugared. The exact rules are given in the report. In your example, the desugaring of x {foo = 23}
is
case x of
PR f b -> PR 23 b
_ -> error "Update error"
Note that the report uses an auxiliary function called "pick" in the desugaring that's described in the section before, 3.15.2, on "Construction using field labels".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With