Reputation: 465
I am a super elm begginer and trying to make app. Currently I am struggling to make landing page and http request to a server. But, I am stuck here...
I have init function something like this below.
init : () -> Url.Url -> Nav.Key -> ( Model, Cmd Msg )
init flags url key =
Model key TopPage
|> goTo (Route.parse url)
The definition of my Model is below.
-- MODEL
type alias Model =
{ key : Nav.Key
, page : Page
, name : String
, tags : List Tag
, jwt : String }
and, goTo function is below.
goTo : Maybe Route -> Model -> ( Model, Cmd Msg )
goTo maybeRoute model =
case maybeRoute of
Nothing ->
( { model | page = NotFound }, Cmd.none )
Just Route.Top ->
( { model | page = TopPage }, Cmd.none )
...
type Route is below.
type Route
= Top
| User String
| Repo String String
parse : Url -> Maybe Route
parse url =
Url.Parser.parse parser url
parser : Parser (Route -> a) a
parser =
oneOf
[ map Top top
, map User string
, map Repo (string </> string)
]
but following error has occured.
-- TYPE MISMATCH -------------------------------------------------- src/Main.elm
This function cannot handle the argument sent through the (|>) pipe:
54| Model key TopPage
55| |> goTo (Route.parse url)
^^^^^^^^^^^^^^^^^^^^^
The argument is:
String -> List Tag -> String -> Model
But (|>) is piping it a function that expects:
Model
What did I make mistake here?....
Upvotes: 0
Views: 506
Reputation: 36375
There are two ways of building a value of a type. Given a simple example of a Person
type alias:
type alias Person = { name : String, age : Int }
You can construct a value by specifying all fields (note that you don't have to specify Person
in the constructor; Elm's compiler is smart enough to know it by its shape):
jane : Person
jane = { name = "Jane", age = 35 }
Or you can build a value by using the type name and specify each field's values in the order in which they were defined. In this style, you can think of Person
acting like a function with two parameters that returns a Person
value.
jane : Person
jane = Person "Jane" 35
In each case, you have to specify all fields of the type when you construct it in order to obtain a complete Person
value. However, that is not the complete story. It is possible to leave off the age parameter when constructing a Person
, but the result isn't a Person
, it's a function that takes an age
and returns a Person
. In other words,
janeAged : Int -> Person
janeAged = Person "Jane"
You can strip off as many parameters from the end as you'd like to make more variations on that constructor, even stripping out all parameters:
somebody : String -> Int -> Person
somebody = Person
Back to your example. You are constructing a Model
value by only specifying two parameters (Model key TopPage
). The value of that expression does not result in a Model
, but in a function that takes three more parameters to create a Model
. And that's why the error message indicated you need three parameters to construct a model.
You need to specify all values of Model
when creating it.
Upvotes: 1
Reputation: 64949
Your Model
type has five fields, but in the line
Model key TopPage
you are only providing values for the first two of the five. You are missing values for the name
, tags
and jwt
fields. Provide values for these and the problem should go away.
When you declare a type alias such as Model
, Elm creates a constructor function also named Model
. Elm functions support partial application, in that if you pass in values for some but not all of the arguments, you end up with a function that takes in the rest of the values. You provided two arguments, so you end up with a function that takes three arguments and returns a Model
.
Upvotes: 2