Changing the order of compilation in an F# project

If you start a new F# project in Visual Studio then the first source file you’ll get is Program.fs with a main function. Then say you add another source file called Domains.fs with the following namespace and type:

namespace com.mycompany.domains.book

type Book = {
    title: string;
    numberOfPages:int;
    author: string
} 
with member this.takesLongTimeToRead = this.numberOfPages > 500

The source file will at first land under Program.fs in the class hierarchy in Visual Studio:

New F# source file in the bottom of the file hierarchy

You then might want to use this Book type in the main function as follows:

namespace com.mycompany.entry

open System
open com.mycompany.domains.book

module Main =    
    [<EntryPoint>]
    let main args =    
        let myBook = {title = "F# for beginners"; numberOfPages = 600; author = "John Smith"}        
        let longTimeOrNot = myBook.takesLongTimeToRead
        printfn "Book title: %s, takes long to read: %b" myBook.title longTimeOrNot
        let keepConsoleWindowOpen = Console.ReadKey()
        0

However this will result in multiple compilation errors and red squiggly lines:

The namespace or module ‘com’ is not defined
The record label ‘title’ is not defined FSharpProjectExamples

…and various others.

The reason is that in F# projects the source files are compiled from top to bottom. Since Domains.fs comes after Program.fs the necessary elements such as the com.mycompany.domains.book namespace haven’t been picked up in Program.fs.

One solution is to reorder the files. Right-click Program.fs and select “Move Down” in the context menu so that it will be the last file in the file hierarchy. The red squiggly lines and compiler errors should disappear. If you still see an exception with the message…

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.

…with the “main” function underlined then clean the solution and rebuild it in Visual Studio. That should solve the problem.

View all F# related articles here.

Advertisements

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

ultimatemindsettoday

A great WordPress.com site

Elliot Balynn's Blog

A directory of wonderful thoughts

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

A Good Blog is Hard to Find

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

Bite-size insight on Cyber Security for the not too technical.

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: