How to generically remove F# Units of measure

China☆狼群 提交于 2019-12-28 07:02:53

问题


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?

回答1:


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



来源:https://stackoverflow.com/questions/412459/how-to-generically-remove-f-units-of-measure

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