Maciej Sitko
Maciej Sitko

Reputation: 416

Elm : initialise set in the model

I am new to Elm. What I want to do is I am trying to initialise model with a set instead of a List, but given elm doesn't have any initialisers for sets (which is a shame, it'd be good if it was #{1,2,3}, as in Clojure), it is problematic.

With the code (Elm tutorial code, a little bit modified), I am trying to

main =
  App.program
    { init = init "cats"
    , view = view
    , update = update
    , subscriptions = subscriptions
    }



-- MODEL


type alias Model =
  { topic : String
  , gifUrl : String
  , error : String
  , history : Set String
  }


init : String -> (Model, Cmd Msg)
init topic =
  ( Model topic "waiting.gif" "" Set.fromList([topic])
  , getRandomGif topic
  )

Which throws me a compiler error of:

Function `Model` is expecting 4 arguments, but was given 5.

Which is strange because this doesn't throw an error in Elm repl and is a valid code in most cases.

How would I achieve this?

Upvotes: 0

Views: 225

Answers (1)

Chad Gilbert
Chad Gilbert

Reputation: 36375

The problem originates from your use of parentheses.

Model topic "waiting.gif" "" Set.fromList([topic])
-- is the same as:
Model topic "waiting.gif" "" Set.fromList [topic] 

You don't need to enclose arguments in parentheses in Elm, but you do need to enclose the entire fourth parameter of Set.fromList [topic] so that the compiler knows it's a single parameter. Change it to this and you should be all set:

Model topic "waiting.gif" "" (Set.fromList [topic])

Upvotes: 4

Related Questions