.NET 9 中引入了接受整数的新 TimeSpan.From*()
重载。 此更改可能会导致 F# 编译器模糊不清,并导致编译时错误。
旧行为
以前,每个 TimeSpan.From*()
方法都有一个重载,即:
- FromDays(Double)
- FromHours(Double)
- FromMicroseconds(Double)
- FromMilliseconds(Double)
- FromMinutes(Double)
- FromSeconds(Double)
新行为
从 .NET 9 开始,添加了接受整数参数的新重载。 在 F# 代码中调用 TimeSpan.FromMinutes(20)
等方法会导致编译时错误:
错误 FS0041:无法根据此程序点之前的类型信息确定方法“FromMinutes”的唯一重载。 可能需要类型批注。 已知类型的参数:intCandidates: - TimeSpan.FromMinutes(minutes: int64) : TimeSpan - TimeSpan.FromMinutes(minutes: int64, ?seconds: int64, ?milliseconds: int64, ?microseconds: int64) : TimeSpan - TimeSpan.FromMinutes(value: float) : TimeSpan
引入的版本
.NET 9 预览版 3
中断性变更的类型
这项更改可能会影响 F# 代码的源兼容性。
更改原因
预先存在的重载接受 Double 参数。 但是,Double 是一种基于二进制文件的浮点格式,因此天然不精确,这可能会导致错误。 此行为导致在 API 表面中出现用户混淆和 bug。 这也是表示此数据效率较低的方法之一。 为了生成预期行为,引入了允许用户传入整数的新重载。
建议的操作
如果这项更改影响 F# 代码,请指定参数类型,以便编译器选择适当的重载。