Skip to content

Instantly share code, notes, and snippets.

@ravicious
Last active August 8, 2016 17:35
Show Gist options
  • Save ravicious/34dc00245cf7edfa95530361b472aeb2 to your computer and use it in GitHub Desktop.
Save ravicious/34dc00245cf7edfa95530361b472aeb2 to your computer and use it in GitHub Desktop.
type alias TimesClicked = Int
type alias Count = Int
updateCount : Msg -> TimesClicked -> TimesClicked
updateCount msg count =
case msg of
Increment ->
if count < maxCount then count + 1 else count
Decrement ->
if count > minCount then count - 1 else count
-- Unfortunately, due to how type aliases work,
-- we could as well write the updateCount type annotation like this…
updateCount : Msg -> Count -> TimesClicked
-- …and it'd still compile.
-- We can fix this by introducing a completely new type instead of an alias:
type Count = Count Int
type TimesClicked = TimesClicked Int
updateCount : Msg -> TimesClicked Int -> TimesClicked Int
updateCount msg count =
case msg of
Increment ->
if count < maxCount then TimesClicked count + 1 else TimesClicked count
Decrement ->
if count > minCount then TimesClicked count - 1 else TimesClicked count
-- With this implementation, we can't mismatch the types,
-- but we have to re-wrap every integer inside updateCount.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment