Guys,
In this post i am going to talk about about the F# application structure.
- If you have worked with any programming languge then i would expect you to know that modules are the building blocks of an application which form the structure of an application. These are the container that contain the F# functions, types and values. We could also think of a module as a static clas in C#
- If F# we could have multiple modules in the same code file but one module could not span multiple code files.
- A module lives in the .Net namespace hierachy. This could be controlled by the programmer as to where does the module resides in the .Net namespace hierarchy. If we are writing a simple F# program we might not see modules being cretaed as that is taken care by the F# comipler internally. Also as you would expect from F# having really good inference capbility, it automatically recognizes the entry function (main function) and structures it.
- If we have multiple possible entry points in the F# application then we need to use the [<EntryPoint>] attribute to define the entry point of the program manually do that the F# compiler recognozes where to start.
- Let see this action. Add the following code to an F# application and run it. We ould see something like the below screen:
- Now lets add a new module in this application. Right click on the project name and select add new item and add a new F# sourcefile and name it Calcy.fs
- Once you hit ok, you would see that the new file is cretaed and the it starts with Module Calcy. Lets do one thing lets cut the first 2 lines of code from our program.fs and paste it below module Calcy in Calcy.fs
- Now in Program.fs we we would see that the compiler is complaining. So what we need to do is prefix the intAdd function with our module name like below:
- But you can see that the error still persists. Wonder why? Its because F# evaluates functions as well as files in the order they appear. So as the Calcy.fs is present below the program.fs in solution explorer we cannot use the functions present in Calcy module. So just right click the Calcy.fs file and click on move up and you see the error disappear from your Program.fs file. We could run the application and see that it works absolutly fine.
- The F# compiler would still automatically create a module from the program.fs file but if want you refer it from a from program in other .Net language then we should know the name of the module and hence it is a good idea to give the module in Prorogram.fs a name. So change the code in Program.fs as below:
- Now here we have multiple things to note. We have specified the module name. We have specified that this main function is the entry point to our application. Also you would see that i have specified a return value of 0 for the main function. This return value is required as the main method expects a return value of type int.
- Now lets modify the Calcy.fs and make it contain 2 modules. You would still see that there is an error which expects us to specify a namspace so that distintion can be made between the modules.
- Lets add the namspace as global and change the code accordingly in Program.fs as the module names have changed and run the app and it should work fine.
- Now if we want to use any other custom namespace other than the global namespace then we need to open that namespace before we can use it otherwise the F# complier will complain. In F# we could also just opn the module instead of the whole namespace and this is an extention the using keyword in the C# language. This gives use the flexibility to remove the module name suffix before the intAdd funstion in Program.fs
- If we modify Program.fs module to be part of the same namespace as Calcy.fs modules we do not need to open the namespace which makes perfect sence.
You can find the source code for this post here
Any questions, comments an feedback are welcome.