elm-codegen icon indicating copy to clipboard operation
elm-codegen copied to clipboard

Support type and type alias constructors

Open tesk9 opened this issue 2 years ago • 2 comments

I'd like to be able to use type and type alias constructors from the Gen folder when working with published Elm packages.

For example, if I had:

type Id
    = Id Int

type alias UserFlags =
    { id : Id
    , username : String
    }

I'd like to be able to use a reference to the Id and UserFlags constructors. Something like:

Gen.Json.Decode.map2 Gen.Example.constructors_.userFlags 
    (Gen.Example.constructors_.id (Elm.int 1)) 
    (Elm.string "username123")

that would produce:

Json.Decode.map2 UserFlags (Id 1) "username123"

Right now, I'm doing the equivalent of:

Decode.map2
    (\a b ->
        Elm.apply
            (Elm.value
                { importFrom = [ "Gen.Helper" ]
                , name = "UserFlags"
                , annotation = Nothing
                }
            )
            [ a, b ]
    )
    (Elm.apply
        (Elm.value
            { importFrom = [ "Gen.Helper" ]
            , name = "Id"
            , annotation = Nothing
            }
        )
        [ Elm.int 1 ]
    )
    (Elm.string "username123")

tesk9 avatar Aug 19 '22 20:08 tesk9

Those exist! But they're probably hard to discover 😅

In the generated file, check out the make_ record. The only difference is that for record type aliases, it doesn't provide the constructor : one -> two -> three -> record form, but the

constructor : { one : Expression, two : Expression, three : Expression} -> Expression 

form.

Could add both if that's useful though

mdgriffith avatar Aug 20 '22 12:08 mdgriffith

Ah! I guess I missed it!

I do think having the constructor : one -> two -> three -> record form in addition would be useful for what I'm trying to do, though, since it can be a bit challenging to switch back and forth from the expression-based api to the inferred type API.

tesk9 avatar Aug 22 '22 15:08 tesk9