Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to list out a certain type definition in elm

Tags:

elm

type alias Employee =
    { role : Role, name : String }


type Role
    = Engineer
    | Manager
    | Sales
    | Accounting
    | Hr


generateSample =
    Role.all |> List.map createModelWithType

I need to accomplish Role.all, which even Role in this case is inaccessible. What's the best way to accomplish / alternative way to express this.

like image 913
user2167582 Avatar asked Jan 24 '26 09:01

user2167582


2 Answers

There is no automatic way to list all constructors of a type. You could build a list like this:

allRoles : List Role
allRoles =
    [ Engineer
    , Manager
    , Sales
    , Accounting
    , Hr
    ]

In Elm, there is no concept of a simple enumeration similar to other languages. Type constructors could also have arguments, which may help in understanding why there is no built-in way to enumerate a list of constructors.

like image 200
Chad Gilbert Avatar answered Jan 27 '26 02:01

Chad Gilbert


There are many considerations when choosing a type. It depends on what behavior will be used with the data. Maybe as you are learning you could simply choose a data type that works and seems simple enough to work with. With experience you will see the advantages of choosing a type over the other.

To get more inspired in the way of thinking in choosing type for different problems you might want to take a look at this presentation: https://www.youtube.com/watch?v=XpDsk374LDE There are two topics mixed in the presentation and one of them is how choosing type for different behaviors.

Here is one way to do it:

type Alias Role = 
     { Engineer : Bool
     , Manager : Bool
     , Sales : Bool
     , Accounting : Bool
     , Hr : Bool
     }
like image 32
VGj. Avatar answered Jan 27 '26 00:01

VGj.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!