Strip Unit of Measure from array

谁都会走 提交于 2020-01-24 11:14:44

问题


I am calling external function requiring float[], but my array is float<m>[]. How could I strip the unit of measure from array?

I need something like the function below, but this does not compile. And I would like to avoid any iterating or duplicating of the array, as float<m> and float values are identical...

let demeasure (arr:float<m>[]): float[] = float[] (arr)

回答1:


I believe that a cast to obj, followed by a dynamic cast to float[] would work, e.g.

(arr :> obj) :?> float[]

because there is no runtime representation.

Possibly see also

F# Units of measure - 'lifting' values to float<something>

How to generically remove F# Units of measure




回答2:


[<Measure>]type m
let f (arr : float[]) : float = 0.0
let arr = [|1.0<m>|]
f (unbox (box arr))



回答3:


let demeasure (arr: float[]) = arr |> Array.map (fun i -> float i)



来源:https://stackoverflow.com/questions/11404027/strip-unit-of-measure-from-array

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