ianmackenzie / elm-units

Simple, safe and convenient unit types and conversions for Elm
https://package.elm-lang.org/packages/ianmackenzie/elm-units/latest/
BSD 3-Clause "New" or "Revised" License
85 stars 14 forks source link

Add Pixels.[int,float,toInt,toFloat] #28

Closed ianmackenzie closed 5 years ago

ianmackenzie commented 5 years ago

Similar to the corresponding functions in Quantity:

Pixels.int : Int -> Quantity Int Pixels
Pixels.float : Float -> Quantity Float Pixels
Pixels.toInt : Quantity Int Pixels -> Int
Pixels.toFloat : Quantity Float Pixels -> Float

A little less flexible than Pixels.pixels/Pixels.inPixels (not generic on number type) but a bit more clear/explicit and read better when used in prefixed form (Pixels.toInt vs. Pixels.inPixels).

Seems like a good general pattern to follow for any quantity type where the units type is synonymous with the quantity itself (pixels, game tiles, grid cells etc.) as opposed to ones where the same quantity can be expressed in many different units (length in meters/centimeters/feet, duration in hours/minutes/seconds, etc.).

ianmackenzie commented 5 years ago

Pixels.fromInt and Pixels.fromFloat might be better - a little inconsistent with Quantity.int and Quantity.float, but it is a slightly different operation: a Quantity Float Unitless is semantically the same as a plain Float value, while a Quantity Float Pixels is not. So it kind of makes sense that the former is constructed by Quantity.float (just "a Float quantity") while the latter would be constructed by Pixels.fromFloat (convert a Float to a number of pixels).

ianmackenzie commented 5 years ago

Problem: what should pixelsPerSecond, inPixelsPerSecond etc. be then? Perhaps best to stick with pixels/inPixels and either:

ianmackenzie commented 5 years ago

Aliasing-to-Quantity (or perhaps even moving everything into a huge Quantity module as suggested in #31) seems more promising - closing this for now.