Search code examples
programming-languagessyntax

Are there any languages that allow units?


When writing the following today in C#

DateTime.Now.AddYears(-60)

I wondered whether there are any languages that allow a more natural syntax with units:

DateTime.Now - 60years

Does anyone know of any? Specifically, I'm interested in the presence of unit operators(?) that turn "60years" into e.g. "TimeSpan.FromYears(60)". It'd also be neat to be able to define your own unit operators, similar to how you can write conversion operators in C#

(Yes, I know TimeSpan doesn't cater for years -- it's an example.)


Solution

  • F# has units of measure. Some examples from

    http://blogs.msdn.com/andrewkennedy/archive/2008/08/20/units-of-measure-in-f-part-one-introducing-units.aspx

    alt text

    alt text