gpt4 book ai didi

.net - 为什么 F# 不支持使用类型缩写扩展系统类型?

转载 作者:行者123 更新时间:2023-12-01 10:30:21 25 4
gpt4 key购买 nike

例如,如果我尝试扩展 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?

关于.net - 为什么 F# 不支持使用类型缩写扩展系统类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43284293/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com