为什么 F# 不支持使用类型缩写扩展系统类型?
Why does F# fail to support extending system type with its type abbreviation?
例如,如果我尝试扩展 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 的回答基本上是正确的 - 默认情况下未实现功能。然而,另一个可能的原因是类型缩写是有范围的,所以缩写的范围是否应该影响扩展的范围并不是很明显。例如:
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?
例如,如果我尝试扩展 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 的回答基本上是正确的 - 默认情况下未实现功能。然而,另一个可能的原因是类型缩写是有范围的,所以缩写的范围是否应该影响扩展的范围并不是很明显。例如:
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?