当程序员抱怨空错误/异常时,有人经常会问我们做什么而没有null.
我对选项类型的酷感有一些基本的想法,但我没有最好的表达它的知识或语言技巧.对于普通程序员可以接近的方式写的以下内容有什么好的解释,我们可以指出那个人?
null programming-languages functional-programming nullpointerexception non-nullable
将 Visual Studio 2019 v16.3.2 与 .NET Core 3.0 项目设置为 C# 8 并启用可为空引用类型。
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<LangVersion>8.0</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)
如果我将其设置为将所有警告视为错误:
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<WarningsAsErrors />
Run Code Online (Sandbox Code Playgroud)
它将其他警告报告为错误。例如,CS0168 The variable 'x' is declared but never used被报告为错误。但所有可为空的引用警告仍报告为警告。例如,CS8600 Converting null literal or possible null value to non-nullable type.仍然报告为警告。
如何将所有可为空的引用警告视为错误?
注意:即使将 CS8600 专门设置为错误处理,也不会导致其报告为错误。如果这样做有效,那么将它们全部视为错误仍然无济于事,因为有很多。
编辑:将特定警告设置为错误放入<WarningsAsErrors>CS8600;CS8602;CS8603</WarningsAsErrors>csproj 并且不起作用。
string name = null; name.ToLower();
在大多数语言中,这样的代码会编译 哪些语言会在编译时捕获此类错误?
我现在知道的唯一一个是elm:http://elm-lang.org