1 Gotchas and Where C# and F# Differ
Craig Oates edited this page 4 years ago

Naming Command-Methods

The commands used in your console program at run-time are bound to F# (and C#) syntax. This is because the commands (the end-user uses) are bound to your command-methods. For example, command method 1 is an invalid command because is it not valid F# (or C#) syntax. CommandMethod1 is a valid command, though. With that said, F# has a much bigger range when it comes to syntax, compared to C#. Because of this, you can name functions using triple quotes. So, you can create a command-method called """command-method-1""" and it will work. You can, also, use back-ticks to declare command-methods -- for example, `command-method-2`. Please refer to "ConsoleCommands.fs" for further comments and code examples.

Writing Command-Methods which Take No Arguments

If you write a command-method which takes no arguments, you must make sure you state that with unit (()). Without it, C.W. will treat your command-method as a typical (C#) variable assignment within the class -- and its typical F# equivalent.

Using Spaces in your Command-Methods

For further information, please refer to the ConsoleCommands module in either the ".Net" or ."Net Core" console projects. You might need them to help explain this next point. The links for these modules are as follows:

To begin, here are two command-methods:

// Example 1
let test () = "The result of the test"

// Example 2
let """test 1""" () = "The result of test 1"

Both of these are valid F#. If you ran your console program with these two command-methods, though, you will notice you cannot run test 1. (Upon second-thought, "access" might be a better choice of word than "run".) It will always resolve to test. You will, also, see no error at compile-time or run-time.

The reason it "works" is because there is a command-method called test. When you enter "test 1" into the prompt, C.W. interprets that input as "test" with the input value "1". This is because test does not take any parameters. So, C.W. ignores the extra input. C.W. only worries about the input when the command-method specifies it requires them. (It does this by including them in the command-method.)

Again, please refer to any of the Console.Commands modules in the repository for further information. Running any of the console programs through a debugger and breaking within Console.Commands might, also, help.