简体   繁体   中英

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.

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. Stepping through this with the debugger it goes from the first (0, "") to printfn "blah" and returns when it hits the second (0,"") . I've checked the spacing several times, and Visual Studio appears to recognize these as two separate expressions.

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. I'm using F# 4.7 with .NET Core 3. What am I missing?

EDIT: Incase it helps, here is how startCycle is called:

    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. The let keyword in F# is used for both. 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 () :

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

This is then called like this: Commands.startCycle ()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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