F#: Entry point of an application
In an attempt to see whether or not the mailboxes I've been working on for my twitter application were actually processing messages on different threads I ran into the problem of defining the entry point of an F# application.
I thought it would be as simple as defining a function called 'main' but I put this function into my code ran the executable and nothing happened!
Googling the problem a bit led me to believe that it is possible to do but that the function needs to be the last thing that happens in the compilation sequence of the project. i.e. it needs to be on the last line of the last file that gets compiled.
That just seemed wrong to me and a Stack Overflow thread suggested that it should be possible to get around this problem by using the EntryPointAttribute on the main function.
I tried this leading to the following code:
[<EntryPoint>] let main args = printfn "in main function" 0
It seems like if you have the EntryPointAttribute on a function that function needs to be of type 'string array -> int' hence the returning of 0 by this function.
This still didn't solve my problem though and when I tried to build the project it was now failing to compile but no errors were showing up on the Visual Studio error list.
This gave me the chance to try out an F# build tool I came across a couple of weeks ago called Fake.
I setup a build file just to compile the code based on Steffen Forkman's blog post and then tried to compile the project, leading to the following error:
error FS0191: A function labelled with the 'EntryPointAttribute' atribute must be the last declaration in the last file in the compilation sequence.
Which suggests that it doesn't actually matter whether or not you have the attribute or not, the main method still needs to be the last step of compilation.
I wanted to get something working so I've just rearranged my fsproj file so that the file with the main function in is the last one listed but it seems a ridiculous fix and I'm sure there must be a better way to do this.
Any ideas?
Mark,
I think what you really need to do is to invoke main as the last line of your file. Think of it this way: when you run your app, it's going to go straight down and run every piece of code. So you can think of everything else in the file as being setup for your main function. Does main actually need to be last? No, but you won't necessarily see the effects of anything defined after its invocation. The common idiom is to do something like this:
let main =
printfn "doing some work…"
main()
Hope this helps. Hit me back if this isn't clear, it's still a little early for me . . .
Sean Devlin
6 May 09 at 10:37 pm
Sorry, formatting didn't work . . . let me try again.
let main =
printfn "doing some work…"
main()
Sean Devlin
6 May 09 at 10:38 pm
[...] notice I am now using [] in the code, since I have multiple files which means you need to be aware of the project file [...]
F#: Discriminated Unions « Tales from a Trading Desk
10 Mar 10 at 12:58 pm