Skip to content

Instantly share code, notes, and snippets.

@homburg
Created March 21, 2016 13:43
Show Gist options
  • Save homburg/96171f16dad363ae75a7 to your computer and use it in GitHub Desktop.
Save homburg/96171f16dad363ae75a7 to your computer and use it in GitHub Desktop.
Elm compiler misleading error
module Counter where
import Html exposing (..)
import Html.Attributes exposing (style)
import Html.Events exposing (onClick)
-- MODEL
type alias Model = Int
-- UPDATE
type Action = Increment | Decrement | Multiply Int | Zero | Mod Int | Round Int
update : Action -> Model -> Model
update action model =
case action of
Increment -> model + 1
Decrement -> model - 1
Multiply n -> model * n
Zero -> 0
Mod n -> model % n
Round n -> (model // n) * n
-- VIEW
view : Signal.Address Action -> Model -> Html
view address model =
body [
style [
("font-family", "Ubuntu Mono, monospace")
, ("font-size", "13px")
]
]
[ div []
[
div [ countWrapper ] [
-- XXX: Bad line
h2 [ countStyle ] [ text (formatNumber (formatNumber model)) ]
-- Correct line
-- h2 [ countStyle ] [ text (formatNumber (toString model)) ]
-- The following line is blamed instead
, h2 [ countStyle ] [ text (toRadix' 2 model) ]
, h2 [ countStyle ] [ text (toRadix' 8 model) ]
, h2 [ countStyle ] [ text (toRadix' 12 model) ]
, h2 [ countStyle ] [ text (toRadix' 16 model) ]
, h2 [ countStyle ] [ text (toRadix' 36 model) ]
]
, button [ buttonStyle, onClick address Decrement ] [ text "-" ]
, button [ buttonStyle, onClick address Increment ] [ text "+" ]
, button [ buttonStyle, onClick address (Multiply 2) ] [ text "×2" ]
, button [ buttonStyle, onClick address Zero ] [ text "0" ]
, button [ buttonStyle, onClick address (Mod 2) ] [ text "% 2" ]
, button [ buttonStyle, onClick address (Round 2) ] [ text "_ 2" ]
]
]
countStyle : Attribute
countStyle = style
[
("padding", "1em")
, ("font-size", "200%")
, ("color", "darkgrey")
]
countWrapper = style
[
("display", "flex")
, ("justify-content", "space-between")
, ("flex-flow", "row wrap")
]
hoverHand = [ ("cursor", "pointer") ]
bold = [ ("font-weight", "bold") ]
buttonStyle =
style
([ ("backgroundImage", "none")
, ("border", "none")
, ("backgroundColor", "darkgrey")
, ("hover:backgroundColor", "black")
, ("hover:color", "white")
, ("text-transform", "capitalize")
, ("padding", "12px 17px")
] ++ hoverHand ++ bold)
formatNumber : String -> String
formatNumber str =
case str of
"0" -> "0"
n -> "et andet tal"
toRadix' : Int -> Int -> String
toRadix' n r = toString n
TYPE MISMATCH
The 2nd argument to function `toRadix'` is causing a mismatch.
45| , h2 [ countStyle ] [ text (toRadix' 2 model) ]
Function `toRadix'` is expecting the 2nd argument to be:
Int
But it is:
String
Hint: I always figure out the type of arguments from left to right. If an
argument is acceptable when I check it, I assume it is "correct" in subsequent
checks. So the problem may actually be in how previous arguments interact with
the 2nd.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment