如何通用删除F#度量单位 [英] How to generically remove F# Units of measure
问题描述
我有一些数据处理代码,它们最终将csv吐出.
I've got some data manipulation code which spits out csv at the end.
我开始对其进行升级以在各处添加度量单位,但是现在我的csv函数遇到了问题:
I started upgrading it to add units of measure everywhere, but I now have a problem with my csv function:
val WriteCSV : string -> 'a list array -> 'b list -> string -> unit
(参数是文件名,列数组,列标题,分隔符)
(the parameters are fileName, column array, column headers, separator)
我以前将[| s; x; y |]发送到WriteCSV的位置,但是现在出现了问题,因为我无法发送[| skm; xmm; ymm |].
Where I previously sent [|s;x;y|] to WriteCSV, I now have a problem, because I can't send [|skm; xmm; ymm|].
我尝试编写一个用于删除度量单位的函数,但是它不起作用.
I tried writing a function for generically removing units of measure, but it doesn't work.
let removeUnit (n:float<_>) = n/1.0<_>
我的问题是:
- 为什么不起作用?
- 可以使它工作吗?
- 还有另一种解决这个特殊问题的方法吗?
推荐答案
如果我对您的问题的理解正确,则将其强制转换为"pure" float将删除该单元. 例如:
If I got your Problem right, casting it to "pure" float removes the Unit. For Example:
[<Measure>] type m
[<Measure>] type km
let removeUnit (x:float<_>) =
float x
let foo = removeUnit 2.6<m>
let foo2 = removeUnit 2.1<km>
val removeUnit:float<'u>-> float
val removeUnit : float<'u> -> float
这篇关于如何通用删除F#度量单位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!