Skip to content

Instantly share code, notes, and snippets.

@thomasballinger
Last active August 18, 2016 23:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomasballinger/a0d8b38fa7186ee2e608d4772f2ebe7e to your computer and use it in GitHub Desktop.
Save thomasballinger/a0d8b38fa7186ee2e608d4772f2ebe7e to your computer and use it in GitHub Desktop.
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