Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support type and type alias constructors #37

Open
tesk9 opened this issue Aug 19, 2022 · 2 comments
Open

Support type and type alias constructors #37

tesk9 opened this issue Aug 19, 2022 · 2 comments

Comments

@tesk9
Copy link
Contributor

tesk9 commented Aug 19, 2022

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")
@mdgriffith
Copy link
Owner

mdgriffith commented Aug 20, 2022

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

@tesk9
Copy link
Contributor Author

tesk9 commented Aug 22, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants