简体   繁体   English

F# 未在表达式结束时返回

[英]F# not returning at end of expression

I have an issue where an F# program is not returning at the end of an expression and ends up executing the next expression below it.我有一个问题,F# 程序没有在表达式末尾返回并最终执行它下面的下一个表达式。

The two expressions as they appear in the file:文件中出现的两个表达式:

   let startCycle = 
        printfn "startCycle"
        (0, "")


   let blah = 
        printfn "blah"
        (0, "")

And when startCycle is called it will print both messages to the console.startCycle时,它会将两条消息都打印到控制台。 Stepping through this with the debugger it goes from the first (0, "") to printfn "blah" and returns when it hits the second (0,"") .使用调试器逐步完成它从第一个(0, "")printfn "blah"并在遇到第二个(0,"")时返回。 I've checked the spacing several times, and Visual Studio appears to recognize these as two separate expressions.我已经检查了几次间距,Visual Studio 似乎将它们识别为两个单独的表达式。

Another weird thing is if I call startCycle multiple times it only prints on the first time through, every call after that results in nothing printed to the console unless I stop and restart the application.另一个奇怪的事情是,如果我多次调用startCycle它只会在第一次打印,之后的每次调用都不会打印到控制台,除非我停止并重新启动应用程序。 I'm using F# 4.7 with .NET Core 3. What am I missing?我正在使用 F# 4.7 和 .NET Core 3。我缺少什么?

EDIT: Incase it helps, here is how startCycle is called:编辑:如果它有帮助,这里是startCycle的调用方式:

    let Run (cmdline: string) : (int * string) =
        let cmodel = parseCmd cmdline
        printfn "%A" cmodel
        match cmodel.Command with
        | "sendMsg4" -> Commands.sendMsg4 cmodel.Args
        | "sendMsg7" -> Commands.sendMsg7 cmodel.Args
        | "sendMsg8" -> Commands.sendMsg8 cmodel.Args
        | "sendMsg10" -> Commands.sendMsg10 cmodel.Args
        | "sendMsg16" -> Commands.sendMsg16 cmodel.Args
        | "sendMsg19" -> Commands.sendMsg19 cmodel.Args
        | "sendMsg22" -> Commands.sendMsg22 cmodel.Args
        | "sendMsg29" -> Commands.sendMsg29 cmodel.Args
        | "sendMixMessages1929" -> Commands.sendMixMessages1929
        | "help" | "Help" -> Commands.help cmodel.Args
        | "startCycle" -> Commands.startCycle
        | "stopCycle" -> Commands.stopCycle
        | "cycleStatus" -> Commands.cycleStatus
        | "set" -> Commands.setStateValue cmodel.Args
        | "show" -> Commands.show cmodel.Args
        | "" -> (1, "")
        | _ -> (-1, "Unknown Command")

startCycle and blah aren't written as functions, they're written as plain values. startCycleblah不是作为函数编写的,它们是作为普通值编写的。 The let keyword in F# is used for both. F# 中的let关键字用于两者。 Don't worry, this is a very common source of confusion for people new to the language.别担心,对于刚接触这门语言的人来说,这是一个非常常见的混淆来源。

To create a function that takes no parameters you need to put in a "dummy" parameter of unit , which is written as () :要创建不带参数的 function ,您需要放入unit的“虚拟”参数,写为()

let startCycle () =
    printfn "startCycle"
    (0, "")

This is then called like this: Commands.startCycle ()然后这样调用: Commands.startCycle ()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM