Skip to content

Field name API? #23

@fumieval

Description

@fumieval

Being able to obtain field names of a record is pretty useful. The following code works but unfortunately GHC can't derive Generic1 instances of higher-kinded types... Do you think barbies' hacks can be applied here?

class FieldNamesB b where
  bfieldNames :: b (Const String)

instance FieldNamesB U1 where
  bfieldNames = U1

instance (FieldNamesB f, FieldNamesB g) => FieldNamesB (f :*: g) where
  bfieldNames = bfieldNames :*: bfieldNames

instance (FieldNamesB t, c ~ 'MetaSel ('Just name) su ss dl, KnownSymbol name, t ~ Const String) => FieldNamesB (M1 S c t) where
  bfieldNames = M1 (Const (fromString (symbolVal (Proxy :: Proxy name))))

instance (FieldNamesB t) => FieldNamesB (M1 C c t) where
  bfieldNames = M1 bfieldNames

instance (FieldNamesB t) => FieldNamesB (M1 D c t) where
  bfieldNames = M1 bfieldNames

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions