Skip to content

Commit

Permalink
Remove 'Fractional'/'Whole' type aliases
Browse files Browse the repository at this point in the history
Not totally happy with the names, and they add another layer of indirection/abstraction in type signatures (which can make compiler error messages more confusing). So leave them out for now, and if needed something like them can be added in the future without triggering a major version bump (as opposed to leaving them in for now and removing/renaming them later, which *would* be a breaking change.)

Note that Quantity.toFractional is no longer required since it can be replaced by 'Quantity.map toFloat'.
  • Loading branch information
ianmackenzie committed Sep 16, 2018
1 parent c3fca38 commit 8b0be53
Show file tree
Hide file tree
Showing 22 changed files with 87 additions and 144 deletions.
30 changes: 6 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,23 +152,11 @@ type Quantity number units
= Quantity number
```

with some convenient type aliases

```elm
-- A fractional number of units, useful for general quantities like length
type alias Fractional units =
Quantity Float units

-- A whole number of units, useful for exact values in cents/pixels
type alias Whole units =
Quantity Int units
```

For example, `Length` is defined as

```elm
type alias Length =
Fractional Meters
Quantity Float Meters
```

This means that a `Length` is internally stored as a `Float` number of `Meters`,
Expand Down Expand Up @@ -293,26 +281,20 @@ own! See [CustomUnits](doc/CustomUnits.md) for an example.

### Understanding Quantity Types

The same quantity type can often be expressed in multiple different ways, which is important to understand especially when trying to interpret error messages! Take the `Speed` type alias as an example. It is defined as

```elm
type alias Speed =
Fractional MetersPerSecond
```

Expanding the `MetersPerSecond` type alias, this is
The same quantity type can often be expressed in multiple different ways. Take
the `Speed` type as an example. It is an alias for

```elm
Fractional (Rate Meters Seconds)
Quantity Float MetersPerSecond
```

Expanding the `Fractional` type alias then gives
but expanding the `MetersPerSecond` type alias, this is equivalent to

```elm
Quantity Float (Rate Meters Seconds)
```

which is the "true" type with no type aliases left to expand.
and you may see any one of these three forms pop up in error messages.

## Getting Help

Expand Down
4 changes: 2 additions & 2 deletions doc/CustomUnits.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ import Quantity
Quantity.sum [ tiles 5, tiles 2.3, tiles 0.6 ]
--> tiles 7.9

pixelsPerTile : Fractional (Rate Pixels Tiles)
pixelsPerTile : Quantity Float (Rate Pixels Tiles)
pixelsPerTile =
pixels 24 |> Quantity.per (tiles 1)

tiles 3 |> Quantity.at pixelsPerTile
--> pixels 72

speed : Fractional (Rate Tiles Seconds)
speed : Quantity Float (Rate Tiles Seconds)
speed =
tiles 12 |> Quantity.per (seconds 1)

Expand Down
10 changes: 5 additions & 5 deletions examples/Eur.elm
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module Eur exposing
, roundUpToNearestCent
)

import Quantity exposing (Fractional, Quantity(..), Whole)
import Quantity exposing (Quantity(..))


type Cents
Expand All @@ -37,21 +37,21 @@ euros numEuros =
Quantity (100 * numEuros)


inEuros : Fractional Cents -> Float
inEuros : Quantity Float Cents -> Float
inEuros (Quantity numCents) =
numCents / 100


roundToNearestCent : Fractional Cents -> Whole Cents
roundToNearestCent : Quantity Float Cents -> Quantity Int Cents
roundToNearestCent (Quantity numCents) =
Quantity (round numCents)


roundDownToNearestCent : Fractional Cents -> Whole Cents
roundDownToNearestCent : Quantity Float Cents -> Quantity Int Cents
roundDownToNearestCent (Quantity numCents) =
Quantity (floor numCents)


roundUpToNearestCent : Fractional Cents -> Whole Cents
roundUpToNearestCent : Quantity Float Cents -> Quantity Int Cents
roundUpToNearestCent (Quantity numCents) =
Quantity (ceiling numCents)
10 changes: 5 additions & 5 deletions examples/Usd.elm
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module Usd exposing
, roundUpToNearestCent
)

import Quantity exposing (Fractional, Quantity(..), Whole)
import Quantity exposing (Quantity(..))


type Cents
Expand All @@ -37,21 +37,21 @@ dollars numDollars =
Quantity (100 * numDollars)


inDollars : Fractional Cents -> Float
inDollars : Quantity Float Cents -> Float
inDollars (Quantity numCents) =
numCents / 100


roundToNearestCent : Fractional Cents -> Whole Cents
roundToNearestCent : Quantity Float Cents -> Quantity Int Cents
roundToNearestCent (Quantity numCents) =
Quantity (round numCents)


roundDownToNearestCent : Fractional Cents -> Whole Cents
roundDownToNearestCent : Quantity Float Cents -> Quantity Int Cents
roundDownToNearestCent (Quantity numCents) =
Quantity (floor numCents)


roundUpToNearestCent : Fractional Cents -> Whole Cents
roundUpToNearestCent : Quantity Float Cents -> Quantity Int Cents
roundUpToNearestCent (Quantity numCents) =
Quantity (ceiling numCents)
4 changes: 2 additions & 2 deletions src/Acceleration.elm
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Acceleration exposing

import Duration exposing (Seconds)
import Length exposing (Meters)
import Quantity exposing (Fractional, Quantity(..), Rate)
import Quantity exposing (Quantity(..), Rate)
import Speed exposing (MetersPerSecond)


Expand All @@ -20,7 +20,7 @@ type alias MetersPerSecondSquared =


type alias Acceleration =
Fractional MetersPerSecondSquared
Quantity Float MetersPerSecondSquared


metersPerSecondSquared : Float -> Acceleration
Expand Down
6 changes: 3 additions & 3 deletions src/Angle.elm
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module Angle exposing
, turns
)

import Quantity exposing (Fractional, Quantity(..))
import Quantity exposing (Quantity(..))


{-| Radians are the standard unit of angle.
Expand All @@ -28,7 +28,7 @@ type Radians
{-| An angle is a fractional number of radians.
-}
type alias Angle =
Fractional Radians
Quantity Float Radians


radians : Float -> Angle
Expand Down Expand Up @@ -91,6 +91,6 @@ atan x =
Quantity (Basics.atan x)


atan2 : Fractional units -> Fractional units -> Angle
atan2 : Quantity Float units -> Quantity Float units -> Angle
atan2 (Quantity y) (Quantity x) =
Quantity (Basics.atan2 y x)
4 changes: 2 additions & 2 deletions src/Area.elm
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ module Area exposing
)

import Length exposing (Meters)
import Quantity exposing (Fractional, Quantity(..), Squared, Whole)
import Quantity exposing (Quantity(..), Squared)


type alias SquareMeters =
Squared Meters


type alias Area =
Fractional SquareMeters
Quantity Float SquareMeters


squareMeters : Float -> Area
Expand Down
4 changes: 2 additions & 2 deletions src/Charge.elm
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ module Charge exposing
, milliampereHours
)

import Quantity exposing (Fractional, Quantity(..))
import Quantity exposing (Quantity(..))


type Coulombs
= Coulombs


type alias Charge =
Fractional Coulombs
Quantity Float Coulombs


coulombs : Float -> Charge
Expand Down
4 changes: 2 additions & 2 deletions src/Current.elm
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ module Current exposing

import Charge exposing (Coulombs)
import Duration exposing (Seconds)
import Quantity exposing (Fractional, Quantity(..), Rate)
import Quantity exposing (Quantity(..), Rate)


type alias Amperes =
Rate Coulombs Seconds


type alias Current =
Fractional Amperes
Quantity Float Amperes


amperes : Float -> Current
Expand Down
4 changes: 2 additions & 2 deletions src/Duration.elm
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module Duration exposing
, weeks
)

import Quantity exposing (Fractional, Quantity(..))
import Quantity exposing (Quantity(..))
import Time


Expand All @@ -32,7 +32,7 @@ type Seconds
time (which would generally be represented by a `Posix` value).
-}
type alias Duration =
Fractional Seconds
Quantity Float Seconds


{-| Find the elapsed time from a start time to an end time. For example,
Expand Down
4 changes: 2 additions & 2 deletions src/Energy.elm
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ module Energy exposing
, megajoules
)

import Quantity exposing (Fractional, Quantity(..))
import Quantity exposing (Quantity(..))


type Joules
= Joules


type alias Energy =
Fractional Joules
Quantity Float Joules


joules : Float -> Energy
Expand Down
4 changes: 2 additions & 2 deletions src/Force.elm
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ module Force exposing
, pounds
)

import Quantity exposing (Fractional, Quantity(..))
import Quantity exposing (Quantity(..))


type Newtons
= Newtons


type alias Force =
Fractional Newtons
Quantity Float Newtons


newtons : Float -> Force
Expand Down
6 changes: 3 additions & 3 deletions src/Length.elm
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module Length exposing
-}

import Quantity exposing (Fractional, Quantity(..))
import Quantity exposing (Quantity(..))


{-| Meters are the standard units of length.
Expand All @@ -37,10 +37,10 @@ type Meters
= Meters


{-| A length is a fractional number of meters.
{-| A length is represented by a number of meters.
-}
type alias Length =
Fractional Meters
Quantity Float Meters


meters : Float -> Length
Expand Down
4 changes: 2 additions & 2 deletions src/Mass.elm
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ module Mass exposing

import Acceleration exposing (MetersPerSecondSquared)
import Force exposing (Newtons)
import Quantity exposing (Fractional, Quantity(..), Rate)
import Quantity exposing (Quantity(..), Rate)


type alias Kilograms =
Rate Newtons MetersPerSecondSquared


type alias Mass =
Fractional Kilograms
Quantity Float Kilograms


kilograms : Float -> Mass
Expand Down
22 changes: 7 additions & 15 deletions src/Pixels.elm
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module Pixels exposing
-}

import Duration exposing (Seconds)
import Quantity exposing (Fractional, Quantity(..), Rate, Squared, Whole)
import Quantity exposing (Quantity(..), Rate, Squared)


type Pixels
Expand Down Expand Up @@ -46,20 +46,12 @@ Note that passing an `Int` will give you a
Quantity Int Pixels
which is equivalent to
Whole Pixels
while passing a `Float` will give you a
Quantity Float Pixels
which is equivalent to
Fractional Pixels
If you pass a _literal_ integer like `1920`, the result can be used as either a
`Fractional` _or_ `Whole` number of pixels.
If you pass a _literal_ integer like `1920`, the result can be used as either an
`Int` _or_ `Float` number of pixels.
-}
pixels : number -> Quantity number Pixels
Expand All @@ -72,22 +64,22 @@ inPixels (Quantity numPixels) =
numPixels


pixelsPerSecond : Float -> Fractional PixelsPerSecond
pixelsPerSecond : Float -> Quantity Float PixelsPerSecond
pixelsPerSecond numPixelsPerSecond =
Quantity numPixelsPerSecond


inPixelsPerSecond : Fractional PixelsPerSecond -> Float
inPixelsPerSecond : Quantity Float PixelsPerSecond -> Float
inPixelsPerSecond (Quantity numPixelsPerSecond) =
numPixelsPerSecond


pixelsPerSecondSquared : Float -> Fractional PixelsPerSecondSquared
pixelsPerSecondSquared : Float -> Quantity Float PixelsPerSecondSquared
pixelsPerSecondSquared numPixelsPerSecondSquared =
Quantity numPixelsPerSecondSquared


inPixelsPerSecondSquared : Fractional PixelsPerSecondSquared -> Float
inPixelsPerSecondSquared : Quantity Float PixelsPerSecondSquared -> Float
inPixelsPerSecondSquared (Quantity numPixelsPerSecondSquared) =
numPixelsPerSecondSquared

Expand Down
Loading

0 comments on commit 8b0be53

Please sign in to comment.