TL;DR
The following type of construction yields compiler errors:
type alias WithId a
= { a | id : String }
type alias NameAndId =
{ id : String, name: String }
type Msg a
= WrapWithId (WithId a)
In the REPL x
and y
defined below compare true
> x : WithId NameAndId
| x = { id = "id", name = "name" }
|
{ id = "id", name = "name" }
: WithId NameAndId
> y : NameAndId
| y = { id = "id", name = "name" }
|
{ id = "id", name = "name" } : NameAndId
> x == y
True : Bool
but comparing u
and v
yields a compiler error (TYPE MISMATCH)
> u = WrapWithId x
WrapWithId { id = "id", name = "name" }
: Msg NameAndId
> v = WrapWithId y
WrapWithId { id = "id", name = "name" }
: Msg { name : String }
Original message
Hi,
I am fairly new to Elm, and I have encountered a compiler error, which seems wrong to me. Can anyone help? Below is code to reproduce the error. I have 3 files: Main.elm
, Other.elm
, and Third.elm
.
The compiler does not seem to like line 37: t1 = Third.Third1 o1
. It complains:
Detected problems in 1 module.
-- TYPE MISMATCH -------------------------------------------------- src\Main.elm
The 1st argument to `Third1` is not what I expect:
37| t1 = Third.Third1 o1
^^
This `o1` value is a:
Other.MyOtherMsg { name : String }
But `Third1` needs the 1st argument to be:
Other.MyOtherMsg Third.MyThird
Hint: Looks like the id field is missing.
If I comment out line 37 and run the code, I can see the value of o1
. It is Other1 { id = "myId", name = "name" }
. So at runtime id
is present, but at compile time it is not?
The Main.elm
file is just the Buttons example given in the Elm guide with line 34-38 added.
Main.elm
module Main exposing (..)
import Browser
import Html exposing (Html, button, div, text)
import Html.Events exposing (onClick)
import Other
import Third
import Debug
-- MAIN
main =
Browser.sandbox { init = init, update = update, view = view }
-- MODEL
type alias Model = Int
init : Model
init =
0
-- UPDATE
type Msg = Increment | Decrement
update : Msg -> Model -> Model
update msg model =
let
mm = Third.newMyThird "myId"
o1 = Debug.log "o1" <| Other.Other1 mm
t1 = Third.Third1 o1
in
case msg of
Increment ->
model + 1
Decrement ->
model - 1
-- VIEW
view : Model -> Html Msg
view model =
div []
[ button [ onClick Decrement ] [ text "-" ]
, div [] [ text (String.fromInt model) ]
, button [ onClick Increment ] [ text "+" ]
]
Other.elm
module Other exposing (..)
type alias MyOther a =
{ a | id : String }
type MyOtherMsg a =
Other1 (MyOther a)
Third.elm
module Third exposing (..)
import Other
type alias MyThird =
Other.MyOther { name : String }
newMyThird : String -> MyThird
newMyThird id =
{ id = id, name = "name" }
type MyThirdMsg =
Third1 (Other.MyOtherMsg MyThird)