gpt4 book ai didi

f# - 这是 F# Builder 错误还是我的误解?

转载 作者:行者123 更新时间:2023-12-04 17:28:29 24 4
gpt4 key购买 nike

当我尝试控制台编程时,我收到了意想不到的结果。

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

printSomeMessage 函数包含在 .cctor() 函数中。这是 IL DASM 结果。
.method 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

所以,它的执行结果是这样的。
Is this the F# BUG?
Args.Length is not two.

我是否缺少一些语法或 F# 特征?还是 F# builder 的 BUG?

最佳答案

不,这是您代码中的错误。您需要在“printSomeMessage”之后添加括号,否则 printSomeMessage 是一个简单的值而不是一个函数。

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

简单值在模块的构造函数中初始化,因此您会看到在模块初始化时调用了您的代码。仔细想想这是合乎逻辑的,简单值的正常情况是将字符串、整数或其他文字值绑定(bind)到标识符。您会期望这会在启动时发生。即以下将在模块启动时绑定(bind):
let x = 1
let y = "my string"

关于f# - 这是 F# Builder 错误还是我的误解?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2400241/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com