Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么F#编译器要检查新创建的数组是否为空?_F# - Fatal编程技术网

为什么F#编译器要检查新创建的数组是否为空?

为什么F#编译器要检查新创建的数组是否为空?,f#,F#,我在玩F#,想看看它是如何生成代码的,与C#相比,我发现了一条奇怪的线。 我正在使用dotTrace来反编译代码,并使C#等价。我还尝试使用LinqPad检查IL代码 我的代码很小 open System [<EntryPoint>] let main argv = let mutable sum = 0 // 1000 or 997 //let arr : int array = Array.zeroCreate 997 //let arr =

我在玩F#,想看看它是如何生成代码的,与C#相比,我发现了一条奇怪的线。 我正在使用dotTrace来反编译代码,并使C#等价。我还尝试使用LinqPad检查IL代码

我的代码很小

open System

[<EntryPoint>]
let main argv =
    let mutable sum = 0
    // 1000 or 997
    //let arr : int array = Array.zeroCreate 997
    //let arr  = Enumerable.Range(0, 997).ToArray()
    let arr :int array = [|0..997|]
    arr |> Array.iter (fun x -> sum <- sum + x)
    printfn "%i" sum
    0
使用.NET4.6版,FSharp.core4.4.0.0版编译,优化代码,生成尾部调用

我对空检查和强制转换非常好奇

(object) numArray == null
我确实理解为什么要做obj演员。数组不是空数组,没有空数组无法检查

我很好奇(不认为这是个问题),问题更多的是关于编译器

  • 为什么检查null会有用?我没有定义选项类型
  • 在什么情况下会触发异常
  • 那张支票是支票的一部分。编译器似乎不够聪明,无法理解在这种情况下,
    box arg
    永远不会是
    null

      // IL_0019: stloc.1      // 'numArray [Range(Instruction(IL_0019 stloc.1)-Instruction(IL_0040 ldloc.1))]'
      // IL_001a: ldloc.1      // 'numArray [Range(Instruction(IL_0019 stloc.1)-Instruction(IL_0040 ldloc.1))]'
      // IL_001b: box          int32[]
      // IL_0020: brfalse.s    IL_0025
      // IL_0022: nop          
      // IL_0023: br.s         IL_0030
      // IL_0025: ldstr        "array"
      // IL_002a: newobj       instance void [mscorlib]System.ArgumentNullException::.ctor(string)
      // IL_002f: throw
    
    (object) numArray == null