Record syntax can be used with newtype
with the restriction that there is exactly one constructor with exactly one field. The benefit here is the automatic creation of a function to unwrap the newtype. These fields are often named starting with run
for monads, get
for monoids, and un
for other types.
newtype State s a = State { runState :: s -> (s, a) }
newtype Product a = Product { getProduct :: a }
newtype Fancy = Fancy { unfancy :: String }
-- a fancy string that wants to avoid concatenation with ordinary strings
It is important to note that the record syntax is typically never used to form values and the field name is used strictly for unwrapping
getProduct $ mconcat [Product 7, Product 9, Product 12]
-- > 756