@opvasger and @malaire, many thanks. I’m sorry for the delay in replying, I’ve been playing with your suggestions (in a sea of interruptions).
@malaire — it’s good to know that Elm requires full initialization of the Model. A much more stable approach than what I was trying to do 
@opvasger,
Using Maybe looks good but it means (I think) that all the functions that consume it must be modified to handle a Maybe input. When I got to the point where I was trying to put a Maybe (Task)
into a package function requiring simply a Task
, I gave up. @mezuzza’s StackOverflow answer to Right way to forcibly convert Maybe a to a in Elm, failing clearly for Nothings has me confused/concerned.
The ‘Model with a custom type’ solution looked simpler. It required a couple of let...in
constructs here and there to destructure the model, and for a while the refactoring was going well, until I got to the following:
type Model
= NoFiles
{}
| WithFiles
{ file : File
, fileList : List File
, text : String
}
…
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
...
TextLoaded string ->
let
[something in here to get the model record]
in
( WithFiles { file = record.file
, fileList = record.fileList
, text = string ++ record.text
}
, Cmd.none
)
To update the model I need to know it’s current values/state. So I need to extract the record payload from the Model’s Constructor {}
structure. And that’s when the fun starts. In a nutshell, how?
I can destructure the model with expressions like Constructor _
or Constructor a
and use this in case statements where it’s the Constructor
that is relevant. But here I want to do it the other way around: the bit I want to ‘ignore into a type variable’ is the Constructor
part, so that I can do something with the record.
In place of [something in here to get the model record]
I tried
( WithFiles record ) = model
( NoFiles nothing ) = model
but the compiler complains that each line doesn’t account for the other situation.
It doesn’t like ( ignoreThis record ) = model
either.
I then tried
record =
case model of
NoFiles empty ->
{}
WithFiles someFiles ->
someFiles
which seemed very reasonable until I got the error message:
This `someFiles` value is a:
{ file : File, fileList : List File, text : String }
But all the previous branches result in:
{}
Of course I might try replacing {}
with a complete record with empty values, but then I’m right back round to where I started because I’m unable to set file
to an ‘empty’ File
because I don’t have access to File
as a constructor.
I’m currently investigating restructuring Model using perhaps nested union types and type variables…
In the mean time, if someone could show me the way out of my self-created labyrinth, that would be very much appreciated.