kevinykuo
kevinykuo

Reputation: 4762

F# casting obj[,] to float[,]

I have an obj[,] whose components are all floats. I want to convert it to a float[,]. I tried many permutations of box, unbox, Array2D.map float, Array2D.copy, System.Convert.ChangeType, etc and none seem to work. How to proceed?

Upvotes: 0

Views: 690

Answers (1)

Mike Zboray
Mike Zboray

Reputation: 40818

This seems to work just fine for me:

let boxedArray = Array2D.init 2 2 (fun x y -> box (float (x + y)))
let unboxedArray = boxedArray |> Array2D.map unbox<float>

printfn "%A" (boxedArray.GetType())  // System.Object[,]
printfn "%A" (unboxedArray.GetType())  // System.Double[,]

Upvotes: 3

Related Questions