C# 对作为编译时常量的未使用变量发出警告:
static void Main(string[] args)
{
var unused = "hey"; //CS0219 The variable 'unused' is assigned but its value is never used
Console.WriteLine("Hello World!");
}
但 F# 编译器不会,尽管编辑器现在确实会识别它:
如果它不仅涵盖编译时常量,还涵盖所有 let 绑定,那么这将在生产中捕获由微不足道的错误引起的真正错误,例如
let callApiXyz connectionInfo = async {
let fullUrl = sprintf "%s..." connectionInfo.Url
...
let! result = httpGet connectionInfo // fail, didn't use the modified url
// Should have been:
// let! result = httpGet { connectionInfo with Url = fullUrl }
...
}
有什么理由不拥有这个(除了“功能不是免费的”)?我觉得这应该是more与 C# 相比,这在函数优先的语言中很重要,因为表达式往往不会产生副作用。
对于我们这些没有 Visual Studio 并手动编辑 fsproj 的人来说,实现 Tomas 答案的方法是
<PropertyGroup>
<OtherFlags>$(OtherFlags) --warnon:1182</OtherFlags>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
作为示例,请参见的项目文件FSharp.Core itself. https://github.com/dotnet/fsharp/blob/d7ebc4a114b19f6e476ae621757e91f66fb4d5d3/src/fsharp/FSharp.Core/FSharp.Core.fsproj
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)