Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
F Sharp (programming language)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Units of measure=== The F# type system supports [[Units of measurement|units of measure]] checking for numbers.<ref name="units-msdn">{{cite web |url=http://msdn.microsoft.com/en-us/library/dd233243.aspx |title=Units of Measure (F#) |access-date=2012-11-24}}</ref> In F#, you can assign units of measure, such as meters or kilograms, to floating point, unsigned integer<ref name="units extended">{{cite web|url=https://github.com/fsharp/fslang-design/blob/main/FSharp-6.0/FS-1091-Extend-Units-of-Measure.md| title=Extend Units of Measure to Include More Numeric Types | website=[[GitHub]] }}</ref> and signed integer values. This allows the compiler to check that arithmetic involving these values is dimensionally consistent, helping to prevent common programming mistakes by ensuring that, for instance, lengths aren't mistakenly added to times. The units of measure feature integrates with F# type inference to require minimal type annotations in user code.<ref name="units">{{cite web |url=http://blogs.msdn.com/b/andrewkennedy/archive/2008/08/29/units-of-measure-in-f-part-one-introducing-units.aspx |title=Units of Measure in F#: Part One, Introducing Units |access-date=2012-11-24}}</ref> <syntaxhighlight lang="fsharp"> [<Measure>] type m // meter [<Measure>] type s // second let distance = 100.0<m> // float<m> let time = 5.0<s> // float<s> let speed = distance/time // float<m/s> [<Measure>] type kg // kilogram [<Measure>] type N = (kg * m)/(s^2) // Newtons [<Measure>] type Pa = N/(m^2) // Pascals [<Measure>] type days let better_age = 3u<days> // uint<days> </syntaxhighlight> The F# static type checker provides this functionality at compile time, but units are erased from the compiled code. Consequently, it is not possible to determine a value's unit at runtime.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)