Created
February 26, 2018 20:19
-
-
Save Arkham/2cf5e12fca027751cc89ba76fc0b10bf to your computer and use it in GitHub Desktop.
Example of phantom type in Elm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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