Establish Units of Measure Relationships With a Quantity of Another Unit

别说谁变了你拦得住时间么 提交于 2020-04-07 04:34:45

问题


I realize that you can express relationships with dimensions of units, like

[<Measure>] type cc = cm^3

and perform meaningful calculations later.

Given some unit of measure type,

[<Measure>] type m

Is it possible to define a unit in a relationship with a quantity of another unit? For example,

// doesn't compile
[<Measure>] type mm = 0.001<m>

// later
let length = 500.0<mm>
let length2 = 0.5<m>
printfn "%A" (length = length2) // prints true

回答1:


In short: no.

Units of measure are annotations on primitives. Period. As you probably know, they will be deleted during compilation.

So here's their fundamental limitation: you cannot attach any kind of functionality to them, because they will all turn into plain old floats.

The compiler will check that your expressions are dimensionally valid, but (for now) it does not automatically generate or insert any sort of 'default' type-conversion functions.

You must write and use those functions yourself, and the best you can do is to make them as straightforward as possible.

Here's how I'd organise your example:

[<Measure>] type mm    
[<Measure>] type mt

// first, I like to define basic functions to quickly annotate dimensionless values
let mm = (*) 1.0<mm>
let mt = (*) 1.0<mt>

// we define a constant conversion
let MmPerMt = 1000.0<mm/mt>

// (though nothing forbids us from defining any conversion we want, and the compiler cannot privilege one over another)
let INeverPaidAttentionInGradeSchool = 12345<mm/mt>

// for ease of use, we bake the conversion constant into functions
let MtToMm = (*) MmPerMt

// usage
let someUserInputInMeters = "12414.23"

let desiredValueInMillimeters = someUserInputInMeters
                                |> float
                                |> mt
                                |> MtToMm


来源:https://stackoverflow.com/questions/33028830/establish-units-of-measure-relationships-with-a-quantity-of-another-unit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!