Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
167 views
in Technique[技术] by (71.8m points)

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 ]
    ]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...