Geeks With Blogs
Mark Pearl

 

Up to now I have been looking at F# for modular solutions, but have never considered writing an end to end application. Today I was wondering how one would even start to write an end to end application and realized that I didn’t even know where the entry point is for an F# application.

After browsing MSDN a bit I got a basic example of a F# application with an entry point

[<EntryPoint>]
let main args =
    printfn "Arguments passed to function : %A" args
    // Return 0. This indicates success.
    0

Pretty simple stuff… but what happens when you have a few modules in a program – so I created a F# project with two modules and a main module as illustrated in the image below…

2010-05-15 05-04-48 PM

When I try to compile my program I get a build error…

A function labeled with the 'EntryPointAttribute' attribute must be the last declaration in the last file in the compilation sequence, and can only be used when compiling to a .exe…

What does this mean? After some more reading I discovered that the Program.fs needs to be the last file in the F# application – the order of the files in a F# solution are important. How do I move a source file up or down? I tried dragging the Program.fs file below ModuleB.fs but it wouldn’t allow me to. Then I thought to right click on a source file and got the following menu.

2010-05-15 05-08-20 PM

 

Wala… to move the source file to the bottom of the solution you can select the “Move Up” or “Move Down” option.

2010-05-15 05-12-18 PM

Now that I got this right I decided to put some code in ModuleA & ModuleB and I have the start of a basic application structure.

ModuleA Code

namespace MyApp
    module ModuleA =            
        let PrintModuleA =         
            printf "hello a \n"
            ()

 

ModuleB Code

namespace MyApp
    module ModuleB =            
        let PrintModuleB =         
            printf "hello b \n"
            ()

 

Program Code

// Learn more about F# at http://fsharp.net
#light
namespace MyApp
module Main =
    open System
        [<EntryPoint>]
        let main args =         
            ModuleA.PrintModuleA                                      
            let endofapp = Console.ReadKey()
            0
Posted on Saturday, May 15, 2010 7:05 PM F# | Back to top


Comments on this post: F# Application Entry Point

# re: F# Application Entry Point
Requesting Gravatar...
Probably one of the most useful F# post I've seen. I had to learn this the hard way!
Left by Michael Crump on May 19, 2010 6:52 PM

# re: F# Application Entry Point
Requesting Gravatar...
Thanks for the article. Seems like manually ordering individual build files is something of a silly design.
Left by Simon on Mar 18, 2013 6:36 AM

# re: F# Application Entry Point
Requesting Gravatar...
Thank you!
Left by Liang on Aug 23, 2013 2:32 PM

# re: F# Application Entry Point
Requesting Gravatar...
god bless you man!
Left by rpk on Apr 03, 2017 11:53 AM

Your comment:
 (will show your gravatar)


Copyright © MarkPearl | Powered by: GeeksWithBlogs.net