HORIZON HASKELLDocslts/ghc-9.14.xd82dee82026-10-12Search names, modules, packages, or :: a typeCtrl K

GHC 9.14.1 · lts/ghc-9.14.x · d82dee8 · 2026-10-12

Modulesafe-wild-cards-1.0.0.2Haskell2010

SafeWildCards

Use -XRecordWildCards safely.

  • 3 values
valuefields :: Name -> PatQ
#

Put all fields of a record constructor into scope.

f $(fields 'Rec) = ... is equivalent to f Rec{..}, but the compiler will warn you about all unused fields. Thus fields brings compile-time safety whenever you want to guarantee that a certain function uses all fields of Rec.

To explicitly ignore a field, match it against _:

f $(fields 'Rec) = ...
  where
    -- Ignored fields
    _ = (recUselessField1, recUselessField2)

Usage examples include ToJSON instances and various encoders in general:

instance ToJSON Rec where
  toJSON $(fields 'Rec) = ...

Note: if you want to define the data type and use fields in the same module, you will need to add $(pure []) after the type declaration. See the post about declaration groups for more details. Your code will look like this:

data Rec = ...
$(pure [])

f $(fields 'Rec) = ...
valuefieldsPrefixed :: String -> Name -> PatQ
#

Like fields, but prefixes all fields with the given prefix.

Useful if you need to put fields from more than one record into scope:

diff :: Rec -> Rec -> Text
diff $(fieldsPrefixed "a_" 'Rec) $(fieldsPrefixed "b_" 'Rec) = ...