Has anyone else found Arrays in Elm to be ‘fragile’?
The other day I managed to ‘crash’ the compiler with a thread blocked indefinitely in an MVar operation
error, by forgetting the element in Array.set
Viz, Array.set index array
instead of Array.set index element array
.
And now I have one of those hard to track down The 1st argument to 'element' is not what I expect
errors — caused (?) by an Array.get
— which doesn’t make sense.
(I’ve replaced some of the usual/obvious with ‘…’ for brevity)
With the following in my code…
type alias Categories =
Array (Maybe String)
type alias Model =
{ categories : Categories
, ...
}
update msg model =
case msg of
...
EditThisCategory catID ->
let
catText =
case Array.get catID model.categories of
Nothing -> ""
Just txt -> txt
in
-- ( { model | categoryBeingEdited = Just catID
-- , categoryTextBeingEdited = catText
-- }
-- , Cmd.none
-- )
( model, Cmd.none )
...
…I get the following when I compile
$ elm make ./src/Main.elm --output ./main.js --debug
Detected problems in 1 module.
-- TYPE MISMATCH -------------------------------------------------- src/Main.elm
The 1st argument to `element` is not what I expect:
34| Browser.element
35|> { ... }
This argument is a record of type:
{ init : Flags -> ( Model, Cmd Msg )
, subscriptions : Model -> Sub Msg
, update :
Msg
-> { categories : Array String
... }
-> ( { categories : Array String
... }
, Cmd Msg
)
, view : Model -> Html Msg
}
But `element` needs the 1st argument to be:
{ init : Flags -> ( Model, Cmd Msg )
, subscriptions : Model -> Sub Msg
, update : Msg -> **Model** -> ( **Model**, Cmd Msg )
, view : Model -> Html Msg
}
The code I’ve presented in update
appears to be where the bug is because, if I comment out everything from let
to in
— leaving only ( model, Cmd.none )
— it all compiles.
Have I made a silly error, or is something strange going on?
I do have two paths in update that end in a direct call to update
that is, update msgX modelY
instead of ( model, cmd )
,
if this is relevant. But they were there, and compiling fine, before this error occurred.