In Elm (or functional languages in general), is it preferable to show 
intent using union types or functions? 

See the code below for an example of what I mean. Forgive the 
sudo/incomplete code....

*Multiple Union Types & One Case Statement*
In this example, the "Req" union types are used to communicate intent. The 
Req type is delivered to the "toCmd" function which is just a large Case 
statement. The toCmd function encapsulates the knowledge about how to 
process each Req.

type Req msg
    = GetApples
    | CreateOrange String
    | PeelBanana String


toCmd req =
  case req of
    GetApples ->
        Task.process Req 
          { settings = {lots of settings...}
          , request = {lots of req config...}
          , decoder = setup decoder...
          }
          
    CreateOrange name ->
        Task.process Req 
          { settings = {lots of settings...}
          , request = {lots of req config...}
          , decoder = setup decoder...
          }
    PeelBanana id ->
        Task.process Req 
          { settings = {lots of settings...}
          , request = {lots of req config...}
          , decoder = setup decoder...
          }


*Multiple Functions and One General Type*
In this example, functions are used to communicate intent. Each function 
encapsulates the knowledge about how to process each request, and the Type 
alias is used to transport that configuration to the toCmd function.  

type alias Req =
  { settings : Http.Settings
  , request : Http.Request
  , decoder : Json.Decode.Decoder a
  }

getApples : Req
getApples =
  Req 
    { settings = {lots of settings}
    , request = {lots of req config}
    , decoder = setup decoder...
    }

createOrange : String -> Req
createOrange name =
  Req 
    { settings = {lots of settings}
    , request = {lots of req config}
    , decoder = setup decoder...
    }

peelBanana : String -> Req
peelBanana =
  Req 
    { settings = {lots of settings}
    , request = {lots of req config}
    , decoder = setup decoder...
    }

toCmd req =
  Task.process req


Any comments or direction would be appreciated.

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to