adding units of measure to the type system for numerical types #1774
Replies: 4 comments 15 replies
-
This is a really cool feature of F#. It'd be grand to explore what this might look like in Gleam. Could we start writing out a design? |
Beta Was this translation helpful? Give feedback.
-
I was just reminded of this and thought about it a little. It's clear that just going back and forth between different delimiters is not helpful. We need to totally change our perspective. pub unit Meter over Int
pub unit Foot over Int
pub unit Second over Int
pub unit MeterPerSecond = Meter / Second
let distance_in_meter: Meter = 8
let distance_in_feet: Foot = 4
let time: Second = 5
let speed_in_meters_per_second = distance_in_meter / time // type -> Meter / Second
let new_speed = 10 as MeterPerSecond + speed_in_meters_per_second
let invalid_speed = speed_in_meters_per_second + distance_in_feet / time // compile error! Now we can have: pub fn calculate_speed(distance: Meter, time: Second) -> Meter/Second {
a/b
} and pub fn calculate_speed(distance: a, time: b) -> a/b {
a/b
} It's also clear that because the return type is using an arithmetic operator, |
Beta Was this translation helpful? Give feedback.
-
ok, I'm just summarizing what options we have, in order of my preference:
As for unit definitions and usage: pub unit Meter
pub unit Second
pub unit Foot
pub unit SpeedUnitWithSecond(d) = d / Second
pub unit AccelerationUnitWithSecond(d) = d / { Second ^ 2 }
pub unit UnitConverter(a, b) = b / a
const jump_height = 4.2 [Meter]
const minimum_speed = 16 [SpeedUnitWithSecond(Meter)]
const acceleration = 4 [AccelerationUnitWithSecond(Meter)]
pub fn calculate_speed(distance: Float\d, time: Float\Second) -> Float\SpeedUnitWithSecond(d) {
distance/time
}
pub fn meters_to_feet(m: Float\Meter) -> Float\Foot {
m *. 3.28 [UnitConverter(Meter, Foot)]
} Did I miss anything? |
Beta Was this translation helpful? Give feedback.
-
Just out of curiosity, has there been any updates or decisions made in regards to unit of measure? It is actually one of the features that made me like F# when one of our newcomers wanted us to try FP in production. It is such a easy sell, especially if you work with science or analysis-like workloads. |
Beta Was this translation helpful? Give feedback.
-
I just found out that F# has this very cool feature where numeric types can have a unit attached to them, so you can say that a integer distance variable is equal to 2 meters which has a different type from another integer distance variable equal to 2 inches.
There are no predefined units in the language, but F# has defined the SI units in its standard library but anything that is not there has to be defined by the developer.
The cool thing is, you can combine these units using arithmetic operators which empowers the compiler to catch some bugs that are usually very elusive.
I understand that some people may think this is an unnecessary feature given the amount of effort needed to implement it in the type system but the main reason this may seem unnecessary to some people is because no other language has this feature and people are just used to not having it. But I think if we have it in gleam and actually use it properly, this can help enormously in a lot of areas.
happy to hear your thoughts!
Beta Was this translation helpful? Give feedback.
All reactions