开发者

Is this the F# Builder bug or my misunderstanding?

开发者 https://www.devze.com 2022-12-22 01:18 出处:网络
When I tried the console programming, I received unexpected result. open System let printSomeMessage = printfn \"Is this the F# BUG?\"

When I tried the console programming, I received unexpected result.

open System

let printSomeMessage =        
    printfn "Is this the F# BUG?"    

[<EntryPoint>]
let main args =    
    if args.Length = 2 then
        printSomeMessage
    else        
        printfn "Args.Length is not two."
    0

The printSomeMessage function was included in .cctor() function. Here is IL DASM result.

.me开发者_如何学编程thod private specialname rtspecialname static 
        void  .cctor() cil managed
{
  // Code size       24 (0x18)
  .maxstack  4
  IL_0000:  nop
  IL_0001:  ldstr      "Is this the F# BUG\?"
  IL_0006:  newobj     instance void class [FSharp.Core]Microsoft.FSharp.Core.PrintfFormat`5<class [FSharp.Core]Microsoft.FSharp.Core.Unit,class [mscorlib]System.IO.TextWriter,class [FSharp.Core]Microsoft.FSharp.Core.Unit,class [FSharp.Core]Microsoft.FSharp.Core.Unit,class [FSharp.Core]Microsoft.FSharp.Core.Unit>::.ctor(string)
  IL_000b:  call       !!0 [FSharp.Core]Microsoft.FSharp.Core.ExtraTopLevelOperators::PrintFormatLine<class [FSharp.Core]Microsoft.FSharp.Core.Unit>(class [FSharp.Core]Microsoft.FSharp.Core.PrintfFormat`4<!!0,class [mscorlib]System.IO.TextWriter,class [FSharp.Core]Microsoft.FSharp.Core.Unit,class [FSharp.Core]Microsoft.FSharp.Core.Unit>)
  IL_0010:  dup
  IL_0011:  stsfld     class [FSharp.Core]Microsoft.FSharp.Core.Unit '<StartupCode$FSharpBugTest>'.$Program::printSomeMessage@3
  IL_0016:  pop
  IL_0017:  ret
} // end of method $Program::.cctor

So, its execution result is like this.

Is this the F# BUG?
Args.Length is not two.

Am I missing some grammar or F# characteristic? Or F# builder’s BUG?


No it's a bug in your code. You need to add parentheses after "printSomeMessage", otherwise printSomeMessage is a simple value rather than a function.

open System

let printSomeMessage() =        
    printfn "Is this the F# BUG?"    

[<EntryPoint>]
let main args =    
    if args.Length = 2 then
        printSomeMessage()
    else        
        printfn "Args.Length is not two."
    0

Simple values are initialized in the constructor of a module, so you see your code being called when the module is initialized. This is logical when you think about it, the normal case of simple values would be binding a string, integer, or other literal value to an identifier. You would expect this to happen a start up. i.e. the following will be bound at module start up:

let x = 1
let y = "my string"
0

精彩评论

暂无评论...
验证码 换一张
取 消