Search code examples
elm

Incorrect View Functon Return Type in elm


I am making a program that changes the rendered text on the screen to be whatever the user inputs in a text box. I think I have the model and the update part of the elm architecture correct, but I really don't understand the view portion.

I'm just having trouble wrapping my head around the square bracket view functions.

Anyway, I am getting this error.

This div call produces:

Html #(Model -> Model)#

But the type annotation on view says it should be:

Html #Msg#Elm

But I am not sure how to change my view function to return Html Msg and I am kinda confused between the difference between that and a string.

Thank you everyone!

Here is my code ...

module Main exposing (..)

import Browser
import Html exposing (Html, div, text, input, Attribute)
import Html.Attributes exposing (..)
import Html.Events exposing (onInput)

main =
  Browser.sandbox { init = init, update = update, view = view }

type alias Model = String

init : Model
init = "Hello, World!"

type alias Msg = String

update : Msg -> Model -> Model
update msg model =
  msg

view : Model -> Html Msg
view model =
  div []
    [ input [ placeholder "Input new string", value model, onInput update ] []
    , div [] [ text model ]
    ]

Solution

  • You're passing the update function as an argument to onInput. Your probably meant to pass it a Msg, which the runtime will then pass to the update function.

    Since your Msg type is an alias for String, you can use onInput identity