A tool to generate a list of custom type constructors

There is an idea to make a tool which will parse all Elm files (e.g. with elm-syntax) and generate an Elm module with the lists of constructors for each custom type. E.g.

From:

module ModuleWithCustomType exposing
    ( FirstType(..)
    , SecondType(..)
    )

type FirstType
    = FirstTypeConstructor1
    | FirstTypeConstructor2
    | FirstTypeConstructor3

type SecondType
    = SecondTypeConstructor1
    | SecondTypeConstructor2

The tool will generate:

module GeneratedConstuctors exposing
    ( moduleWithCustomTypeFirstTypeList
    , moduleWithCustomTypeSecondTypeList
    )

import ModuleWithCustomType


moduleWithCustomTypeFirstTypeList : List ModuleWithCustomType.FirstType
moduleWithCustomTypeFirstTypeList =
    [ FirstTypeConstructor1
    , FirstTypeConstructor2
    , FirstTypeConstructor3
    ]

moduleWithCustomTypeSecondTypeList : List ModuleWithCustomType.SecondType
moduleWithCustomTypeSecondTypeList =
    [ SecondTypeConstructor1
    , SecondTypeConstructor2
    ]

Sometimes there is a need to use a list of custom type constructors. Such a tool can make sure that a list of custom type constructors is always up to date.

Do you folks think it will be useful for anyone?

1 Like

It’s certainly possible, and probably even desirable. But do you actually need it right now to get something done? Working from concrete examples is much better for discussion, and will lead to a better tool. :smile:

2 Likes

Thank you for reply. I don’t need it right away, it’s always possible to have a list manually created. I know it’s possible and easy to do. I would do it myself, but don’t have time for that unfortunately. So I thought to delegate it. I know there a lot of folks who are looking for a way to contribute to Elm community. So I raised an idea to see if it’s useful and if so someone could create it. I can myself make a technical specification and review the code if required.

I saw this issue with a list of type constructors was raised few times. So an external tool to automatically generate such a list can be handy.

At work we have a lot of tools to automatically generate Elm modules with all kind of stuff, and it works great for us.

This is not really The Elm Way™. External tools are valuable and something similar to what you propose here might be very useful BUT, the best way to start it is from practical needs.

Take a look at this short video about Value Proposition Design.

Approaching an elm tool should be similar. Some jobs that the elm programer is trying to do should be identified, pains and gains around those jobs should be identified and then a solution designed to address the issues identified.

The value that the tool provides has to be large enough that the development and maintenance costs are offset by it.

There are some places where I need the types converted into lists. Most of the time this accomplished without pain by just copy&paste plus some multi-selection edit to replace | with a ,. It takes just a few seconds.

This kind of functionality might work wonderfully within editor plugins.

2 Likes

Surely you can do that inside your editor with a few shortcuts/macros, a full blown tool is overkill for this kind of task.

I’ve seen a little trick being used to get a compiler error when you change your types but forget to update the list all the constructors. It’s not perfect, because you can still omit a constructor in the list below, but at least you’ve been warned:

moduleWithCustomTypeSecondTypeList : List ModuleWithCustomType.SecondType
moduleWithCustomTypeSecondTypeList =
    let
        _ =
            case SecondTypeConstructor1 of
                SecondTypeConstructor1 ->
                    ()

                SecondTypeConstructor2 ->
                    ()
    in
    [ SecondTypeConstructor1
    , SecondTypeConstructor2
    ]
5 Likes

Why do you think it’s not the Elm way? There are many examples where a tool to generate Elm module maybe useful. E.g. turn JSON file with translations into Elm module.

The pain is that a list of constructors is created manually. The gain is that a tool can be simply added to build task and guarantee to always have a full list of constructors.

I do agree here, that’s why I raised the question. It’s quite a small tool. So I’m interested to gather feedback if anyone else founds it painful to update a list of constructors or not really?

E.g. I remember @Janiczek made a post and another post about a list of constructors before. So there is definitely pain there.

Yes, the work to convert constructors of a custom type into a list is very straight forward. The question here really is if it can be automated, why not? Also if it’s a part of editor plugin how would you guarantee it works the same way company wide?

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.