Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type question on form.html #882

Open
agreif opened this issue Dec 7, 2022 · 1 comment
Open

Type question on form.html #882

agreif opened this issue Dec 7, 2022 · 1 comment

Comments

@agreif
Copy link

agreif commented Dec 7, 2022

on the page 'https://guide.elm-lang.org/architecture/forms.html' is the following type annotation:

viewValidation : Model -> Html msg

should this not be with capital 'M'

viewValidation : Model -> Html Msg

?

Why is 'msg' as type allowed?

thanks

@SyntacticalAnomaly
Copy link

SyntacticalAnomaly commented Apr 26, 2023

Correct me if I'm wrong, but my understanding of this is that lowercase 'msg' is a type variable. It could just as easily be 'a' or 'insertSomeMsgHere' for all intents and purposes. Unless Elm knows what specific type is going to be returned there, it doesn't actually care what that type is.

'Msg' on the other hand is specifically a type that someone has declared in the program. It's the type that gets passed into your update function, and if it doesn't match the message that your Html in your view wants to output, it won't compile.

In this specific example, neither viewInput or viewValidation actually output a 'Msg' which allows us to use a type variable. It might seem like viewInput is returning a 'Msg' at first glance, but it's actually asking for the 'Msg' to be passed in as an argument (toMsg) and as such needs to use a 'msg' (type variable) in its type declaration, just in case a 'Msg' is not the intended output.

If I were to rewrite the Msg definition as this:

type OtherMsg
    = Name String
    | Password String
    | PasswordAgain String

With viewInput defined as it is, only the type annotations for the view and update functions would need to be changed from

view: Model -> Html Msg

update: Msg -> Model -> Model

to

view: Model -> Html OtherMsg

update: OtherMsg -> Model -> Model

I hope that helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants