Type signature for update and view


#1

Hey everyone, I’ve been playing around with Elm, but I’m getting this compile error that I don’t understand: https://ellie-app.com/4qQRZhfJ6tLa1

Don’t mind if code seems a bit weird, I’ve been cutting things out unrelated to the error I’m getting. Can anyone explain why I get an error saying that view and update should only take Msg and no Model?


#2

init should return (Model, Cmd Msg), not (Msg, Cmd Msg). For some reason the compiler error got a bit confusing.


#3

Herteby is right, I made some changes, you can go from there, it compiles now, https://ellie-app.com/4qR8gh4dZGNa1, your model should be an alias type (record type), you defined your model as a custom type


#4

The Model doesn’t have to be a record, it can be whatever you want. A record just tends to be the most practical.


#5

Ok, good point, thanks, as you said, but most common type for modeling Models is a record type, I never had to use a custom type as a model itself.

https://ellie-app.com/4qQRZhfJ6tLa1


#6

Here is a model as custom type


#7

Thanks! I guess the compiler can’t really know better when element is generic, and init is the first function it sees, and reduces the generic arguments based on the types provided there. Oh well ¯\(ツ)


#8

The custom type was on purpose, since it allows me to encode my GUI as a nice graph, where the nodes are the variants of my Model and my Msgs are the edges connecting the nodes.

Once I start having state I want to share between different views, I imagine I’ll do something like this:

type alis Model = 
{ view : ViewCustomType
, dataStore : SharedDataModel
}

That way I can encode both :slight_smile:


closed #9

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.