F#选项......它们是否真的阻止了空引用异常

Kno*_*uch 7 f#

我正在阅读"Professional F#2.0"一书.作者展示了以下代码

let a string : option = None
if a.IsNone then
    System.Console.WriteLine("a is none")
else
    System.Console.WriteLine("a is some");;
Run Code Online (Sandbox Code Playgroud)

然后说

"这使得Option的使用远远优于null的使用,并且在很大程度上消除了在运行时抛出的重要异常源"

好.所以我写

System.Console.WriteLine(a.GetType());;
Run Code Online (Sandbox Code Playgroud)

我明白了

System.NullReferenceException:未将对象引用设置为对象的实例.at System.Object.GetType()at.$ FSI_0008.main @()由于错误而停止

我就像'un !!!'

真的是怎么做的

if a.isSome then
    do bla bla

any different from

if a != null then
   do bla bla
Run Code Online (Sandbox Code Playgroud)

所以我不知道程序员是如何从NullPointers中保存的

PS:NullPointerException过去给我带来了很多悲伤.

Tom*_*cek 9

F#编译器不会完全阻止您NullReferenceException.当您使用.NET中定义的类型时,您仍然可以获得null价值,因为F#无法阻止它.

但是,当您使用F#中声明的类型时,编译器不允许创建null该类型的值,因此NullReferenceException在这种情况下避免使用.例如,以下代码无法编译:

type Person(name:string) = 
  member x.Name = name

// 'Person' is a type declared in F#, so the argument cannot be 'null' in safe code
let printName (person:Person) = 
  printfn "%s" person.Name

// Compiler error here - 'null' is not a valid value of 'Pereson' type
printName null
Run Code Online (Sandbox Code Playgroud)

当您option<Person>用作参数时,您必须明确检查NoneSome案例.这是最好的使用match,它会检查您是否没有遗漏任何案例.例如:

let printName (person:option<Person>) = 
  match person with
  // You get a warning here, saying that you're not handling the 'None' case!
  | Some person -> printfn "%s" person.Name
Run Code Online (Sandbox Code Playgroud)

警告告诉您应该添加案例处理None.您仍然可以编译代码,但NullReferenceException如果不忽略警告,则在使用F#类型时将无法获得.

另请参阅这篇伟大的,相关的StackOverflow帖子.


mis*_*tor 5

为了增加Tomas的答案,Option类型的一个主要好处在于它支持的高阶函数,它可以提供更多的简洁性和安全性.您可能会发现我的博客文章对该主题很有用.