例如,如果我尝试用扩展int
而int
不是类型的真实名称,则此代码将失败:
type int with
member this.IsEven = this % 2 = 0
我必须System.Int32
改用:
type System.Int32 with
member this.IsEven = this % 2 = 0
//test
let i = 20
if i.IsEven then printfn "'%i' is even" i
为什么不能使用类型缩写int
?
我认为Will的glib答案基本上是正确的-默认情况下未实现功能。但是,另一个可能的原因是类型缩写的作用域是范围,因此,缩写的范围是否会影响扩展的范围尚不是很明显。例如:
module X =
type t = int
module Y =
// imagine we could hypothetically do this
type X.t with
member i.IsEven = i % 2 = 0
open Y
fun (x:System.Int32) -> x.IsEven // should this compile even though t isn't in scope here?
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句