elm-generic-dict
can be used to codegen dictionaries with arbitrary types as keys. It is meant to be used as a library with elm-codegen
.
The dictionaries don't contain functions, and the API (of the generated code) doesn't require you to pass any function.
The main disadvantage of using this approach is code duplication, while the advantages are:
- does not contain functions (useful for debugging and usage in lamdera),
- does not require passing in functions when using it (cleaner API, no possibility of mistakes),
- live code inclusion will trim unused functions.
Say you'd like a Dict
of UUID.UUID
:
- First, follow elm-codegen's docs, on installing packages
- edit the
codegen/Generate.elm
to look something like
module Generate exposing (main)
import Elm
import Elm.Annotation as Type
import Gen.CodeGen.Generate as Generate
import Gen.UUID
import GenericDict
main : Program {} () ()
main =
Generate.run
[ customDictFile
]
customDictFile : Elm.File
customDictFile =
GenericDict.init
{ keyType = Gen.UUID.annotation_.uUID
, namespace = []
, toComparable = Gen.UUID.toString
}
|> GenericDict.withTypeName "UuidDict"
|> GenericDict.generateFile
- run
elm-codegen run
to have it create thegenerated/UuidDict.elm
- update your
elm.json
to includegenerated/
as asource-directory
- import it as
Import UuidDict exposing (UuidDict)
to use it like aUuidDict a