【发布时间】:2015-10-15 11:51:29
【问题描述】:
以下 F# 强制转换运算符有什么区别?我似乎无法理解它们为什么以及如何不同。
(type) X
X :> type
X :?> type
【问题讨论】:
-
这只涵盖Upcast和Downcast。
标签: casting f# type-conversion
以下 F# 强制转换运算符有什么区别?我似乎无法理解它们为什么以及如何不同。
(type) X
X :> type
X :?> type
【问题讨论】:
标签: casting f# type-conversion
第一个不是 F# 中的强制转换,但如果您习惯 C#,它可能看起来像一个。但这实际上是在调用类型转换函数(如int),而括号实际上并不是必需的(并且可能会使一切变得更加混乱)。
(int) "4" // the number 4 - this is a conversion, not a cast
int "4" // same thing, but idiomatic
int "NaN" // compiles but throws an exception at runtime
(int) (box 4) // doesn't compile because int doesn't do downcasts, just known conversions
请注意,这适用于原始类型,因为有预定义的转换函数,但它不适用于任意类型:
(bigint) 1 // no such conversion function, so this is a compile-time error
另外两者的区别在于:> 执行向上转型(从类型到超类型,这总是安全的),:?> 执行向下转型(从类型到子类型,这可能会失败,因此 @ 987654326@在中间)。
还有命名为upcast 和downcast 的运算符可以以类似的方式使用:
5 :> obj // upcast int to obj
(upcast 5 : obj) // same
(box 5) :?> int // downcast an obj to int (successfully)
(downcast (box 5) : int) // same
(box "5") :?> int // downcast an obj to int (unsuccessfully)
在某些情况下,向上转换或向下转换的目标类型可能会被成功推断,在这种情况下,使用 upcast 或 downcast 运算符时不需要类型注释,而您始终需要提供类型:> 或 :?> 运算符的参数(尽管您可以提供 _ 如果您希望它被推断):
List.map (fun x -> x + 1) (downcast (box [1]))
List.map (fun x -> x + 1) (box [1] :?> _)
【讨论】:
obj 转换为具有特定运算符可用的东西,例如*?我知道您可以通过具有相当复杂的代码<'T when 'T : (static member (+) : ^T * ^T -> ^T)> 来制定通用约束来做到这一点,但是有没有办法将其转换为那个?