Skip to content

Modifying field names in generated Haskell types #2262

Closed
@mgoldenberg

Description

@mgoldenberg

I have a set of Dhall types which I recently started generating in Haskell with Dhall.TH.makeHaskellTypes. This is a really handy tool, but there are certain cases where the mechanism breaks down. For example, Dhall and Haskell don't have the same restrictions on naming. Given that the Haskell types are generated without modification from the Dhall types, one can run into naming issues which prevent proper Haskell code from being generated.

Consider the following (contrived) example. Given a Dhall file, Foo.dhall, with contents:

let Foo = { type : Natural } in Foo

when generating this type with

Dhall.TH.makeHaskellTypes [ Dhall.TH.SingleConstructor "Foo" "MakeFoo" "./Foo.dhall" ]

we get the following generated code

data Foo = MakeFoo { type :: GHC.Natural.Natural }

This code doesn't compile, of course, because type is a reserved word in Haskell. In some cases it may be easy to just rename the field to something that is a proper field name in Haskell; however, renaming a field in the Dhall type is not always straightforward if you're in later stages of a project and are already depending on a particular field name in other places prior to having generating the Haskell code.

I was wondering if there is an easy way to modify the field name that is generated in the Haskell code. If not, perhaps there could be another function for doing so. For instance, one could add a prefix to all fields generated, like so:

Dhall.TH.makeHaskellTypesWithFieldModifier 
    [ Dhall.TH.SingleConstructor "Foo" "MakeFoo" "./Foo.dhall" (\field -> "foo" ++ [toUpper . head $ field] ++ tail field) ]

which might generate

data Foo = MakeFoo { fooType : GHC.Natural.Natural }

Or perhaps instead of a single function, one passes a record that has all sorts of settings for how to generate the Haskell type.

Any thoughts or help would be greatly appreciated!

Metadata

Metadata

Assignees

No one assigned

    Labels

    marshallingMarshalling between Dhall and Haskell

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions