Skip to content

Instantly share code, notes, and snippets.

@Arkham
Created February 26, 2018 20:19
Show Gist options
  • Save Arkham/2cf5e12fca027751cc89ba76fc0b10bf to your computer and use it in GitHub Desktop.
Save Arkham/2cf5e12fca027751cc89ba76fc0b10bf to your computer and use it in GitHub Desktop.
Example of phantom type in Elm
module Main exposing (main)
import Html exposing (Html, text)
type Distance a
= Distance Float
type Kilometer
= Kilometer
type Mile
= Mile
marathonDistance : Distance Kilometer
marathonDistance =
Distance 42.195
distanceKmToMiles : Distance Kilometer -> Distance Mile
distanceKmToMiles (Distance km) =
Distance (0.621371 * km)
marathonDistanceInMiles : Distance Mile
marathonDistanceInMiles =
distanceKmToMiles marathonDistance
main : Html msg
main =
text (distanceKmToMiles marathonDistanceInMiles)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment