如何一般地删除 F# 度量单位 [英] How to generically remove F# Units of measure
问题描述
我有一些数据操作代码,最后会输出 csv.
我开始升级它以在任何地方添加度量单位,但现在我的 csv 函数有问题:
val WriteCSV : string ->'一个列表数组 ->'b 列表 ->字符串 ->单元
(参数为文件名、列数组、列标题、分隔符)
我之前将 [|s;x;y|] 发送到 WriteCSV 的地方,现在遇到了问题,因为我无法发送 [|skm;毫米;嗯|].
我尝试编写一个函数来删除度量单位,但它不起作用.
let removeUnit (n:float<_>) = n/1.0<_>
我的问题是:
- 为什么不起作用?
- 它可以工作吗?
- 还有其他方法可以解决这个特定问题吗?
如果我的问题是正确的,将其转换为纯"浮点数会移除单元.例如:
[] type m[<Measure>] 类型公里让 removeUnit (x:float<_>) =浮动 x让 foo = removeUnit 2.6 m让 foo2 = removeUnit 2.1<km>
<块引用>
val removeUnit : float<'u> -> float
I've got some data manipulation code which spits out csv at the end.
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)
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<_>
My questions are:
- Why doesn't it work?
- Can it be made to work?
- Is there another way to solve this particular problem?
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
这篇关于如何一般地删除 F# 度量单位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!