Skip to content

Instantly share code, notes, and snippets.

@thomasballinger

thomasballinger/Main.elm

Last active Aug 18, 2016
Embed
What would you like to do?
This type error goes away if I switch the order of lines 54 and 55
-- TYPE MISMATCH -------------------------------------------------- src/Main.elm
The branches of this `if` produce different types of values.
48|> if True then
49|> model
50|> else
51|> { model
52|> | fieldOfBothTypes =
53|> model.fieldOfBothTypes
54|> |> hasBothXAndY
55|> |> hasX
56|> }
The `then` branch has type:
{ ..., fieldOfBothTypes : HasX { ..., x : ... } }
But the `else` branch is:
{ ..., fieldOfBothTypes : HasX { ... } }
Hint: These need to match so that no matter which branch we take, we always get
back the same type of value.
Detected errors in 1 module.
module Main exposing (..)
type alias HasXAndY a =
{ a | x : Float, y : Int }
type alias HasX a =
{ a | x : Float }
type alias Model =
{ z : Float
, fieldOfBothTypes : HasX (HasXAndY {})
}
hasBothXAndY : HasXAndY (HasX a) -> HasXAndY (HasX a)
hasBothXAndY thing =
thing
hasX : HasX a -> HasX a
hasX thing =
let
unused =
thing.x
in
thing
inferHasZ : Model -> Model
inferHasZ model =
let
unused =
model.z
in
model
foo : Model -> Model
foo model =
case True of
True ->
inferHasZ model
False ->
if True then
model
else
{ model
| fieldOfBothTypes =
model.fieldOfBothTypes
|> hasBothXAndY
|> hasX
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment