动态转换时出错,静态转换工作

时间:2014-02-04 02:27:32

标签: .net casting f#

System.Collections.Generic.List<int>投射到int seq。我认为这应该很容易在静态和动态的情况下工作,我认为在静态中工作的任何应该在动态中工作,但这是结果我'得到:

静态:

> List<int>() :> int seq;;
val it : seq<int> = seq []

动态:

> List<int>() :?> int seq;;

  List<int>() :?> int seq;;
  ^^^^^^^^^^^^^^^^^^^^^^^

stdin(115,1): warning FS0067: This type test or downcast will always hold

  List<int>() :?> int seq;;
  ^^^^^^^^^^^^^^^^^^^^^^^

stdin(115,1): error FS0193: Type constraint mismatch. The type 
    seq<int>    
is not compatible with type
    List<int>    
The type 'seq<int>' is not compatible with the type 'List<int>'

这里发生了什么?

2 个答案:

答案 0 :(得分:2)

问题是方向之一。

使用:>你去“obj” - 一个永远成功的演员。

使用:?>你会朝相反的方向前进,而演员表可能会失败。

因此,您在示例中不能使用:?>,因为您的方向错误。

我认为将它们视为静态和动态转换是错误的。使用upcast和downcast使得区别更加明显。

答案 1 :(得分:0)

这是F#语言中的已知问题。它目前阻止动态转换为除object之外的任何接口。 Don在这里评论说,这个限制将在未来版本中删除

  

http://cs.hubfs.net/topic/None/57012