System.Array []与F#中的float [] []不兼容 [英] System.Array [] not compatible with float [] [] in F#

查看:41
本文介绍了System.Array []与F#中的float [] []不兼容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要调用一个函数,该函数将 System.Array [] 作为F#中的一个参数.(该函数在库中).我需要传递 float [] [] [] 类型的参数,但是编译器拒绝编译.为了复制该问题,我编写了以下代码

I need to call a function that takes System.Array [] as one parameter in F#. (The function is in a library). I need to pass an argument of type float [] [] [] but the compiler refuses to compile. To replicate the problem, I wrote the following code

let x : float [] [] = Array.init 2 (fun x -> Array.zeroCreate 3)
x :> System.Array;; // This is OK

val x : float [] [] = [|[|0.0; 0.0; 0.0|]; [|0.0; 0.0; 0.0|]|]

> x :> System.Array [];; //Error

  x :> System.Array [];;
  ^^^^^^^^^^^^^^^^^^^^

stdin(14,1): warning FS0059: The type 'System.Array []' does not have any proper subtypes and need not be used as the target of a static coercion

  x :> System.Array [];;
  ^^^^^^^^^^^^^^^^^^^^

stdin(14,1): error FS0193: Type constraint mismatch. The type 
    float [] []    
is not compatible with type
    System.Array []    
The type 'System.Array' does not match the type 'float []'

我该如何解决这个问题?

How can I solve this problem?

谢谢.

推荐答案

您可以执行以下操作:

let x : float [] [] = Array.init 2 (fun x -> Array.zeroCreate 3)

let toArray (xs : #System.Array []) =
    Array.map (fun x -> x :> System.Array) xs

let x' : System.Array [] = toArray x

这篇关于System.Array []与F#中的float [] []不兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆