I recently asked a question in the beginner channel on Elm Slack and the really great answers led me to further speculation about the function and behaviour of the init
function in Main.elm
.
If our
Model
has many fields but we only want toinit
a subset of those how can we tell the compiler to only look for those?
The question can be restated as:
Why does
init
demand that we instantiate the entire Model when we might only want to instantiate a tiny piece of our program - which might be a UI shell waiting for user interaction?
The answers have led me to ask if the following is correct:
- Does
init
demand the entire Model be presented to it in some way because its function is to instantiate the entire program state into the Elm Runtime and construct the Virtual Dom? - If we do not wish to duplicate the entire
Model
intoinit
. If we do not wish to instantiate all of our state at startup. And we do not wish to hack a work-around to this requirement byinit
to present everything that exists in theModel
(now and in the future as we add to the program) is there one particular pattern or solution that has risen to the top of best practices in the Elm community to solve this? - Does this best practice include the use of Modules to convert
Main.elm
into a high-level FSM using Elm’s type system? Also making impossible states impossible and providing guarantees for application startup? Where theModel
is turned into a Custom Type that allows us to present only that part of the program we wish to instantiate at startup?
There are four Elm talks that come to mind in all this:
Charlie Koster created an article on Medium that adds to this approach, although he writes that it was just an idea and a work in progress at the time.
I understood Life of a File as a recommendation to only move to Modules when data naturally evolved into groups of types that could be moved out of Main.elm
together. However, it seems that, especially if we use something like domain-driven type design, and given the considerations that Evan draws our attention to, it might also be good to move to using Modules and more sophisticated patterns sooner rather than later (or as a matter of course in contemporary Elm practices).
This is a very beginner minded exploration so I’ll edit this OP as required to improve clarity of what I’m asking as responses are posted.