Hi,
the difference from C# is that in F# the compilation does depend on the order of source files and a code can "see" only types/functions/modules declared earlier. This means that if you have "bar.fs" & "foo.fs", the code in "foo.fs" can use declarations from "bar.fs", but not vice versa. (Rreordering files in VS is unfortunately currently a bit painful - you can either edit F# project file by hand or rename the files in solution explorer, which moves the renamed file to the end of the list).

You don't need to create FSI files or mark declarations as public - by default, everything will be public and FSI files and public/private annotations are only useful for hiding implementation (which may be good for a library, but can be done later if needed).

The simples example is following:

1
2
3
4
5
6
7
8
 
// 1. file: bar.fs
let someFunc a b = a + b

// 2. file: foo.fs
// open "Bar" module - by default F# uses name of the file with first letter in uppercase
open Bar 
do printf "%d" (someFunc 1 2)

More advanced options are to use "module SomeName" or "namespace SomeName" at the beginning of the source file, which tells the compiler to put everything into a module or namespace with the specified name (the difference is that namespace cannot contain functions and module looks like a class with static members if used from C#).

You can also use nested modules - e.g. define a module "Mod" in namespace "MyStuff", which also contains class "Cls" looks like this:

1
2
3
4
5
6
7
8
9
 
#light
namespace MyStuff

type Cls() =
  // .. some class members

module Mod = 
  // .. some functions in the module

When writing code in F# I usually don't place every class into a single file (managing the order of files would be difficult), but I usually split the project into larger groups of related classes & modules and use a singe file for every group of things. You also need to make sure that classes/functions that recursively depend on each other will be in a same file. This may look a bit annoying, but it can help to make code cleaner (but I'm not saying that C#-like compilation mode wouldn't be nice sometimes :-) ).

Hope this helps!

By on 3/19/2008 6:45 PM ()

reordering files in VS is unfortunately currently a bit painful - you can either edit F# project file by hand or rename the files in solution explorer, which moves the renamed file to the end of the list

Doesn't alt-[up or down arrow] when the file is selected in the Solution Explorer reorder in Visual Studio 2010 rc?

By on 2/19/2010 4:29 PM ()

reordering files in VS is unfortunately currently a bit painful - you can either edit F# project file by hand or rename the files in solution explorer, which moves the renamed file to the end of the list

Doesn't alt-[up or down arrow] when the file is selected in the Solution Explorer reorder in Visual Studio 2010 rc?

Yes; note that Tomas' message was from March 2008, way back before any of the current project system was implemented. :)

By on 2/19/2010 6:39 PM ()

Thanks for the tip, Tom. I was able to edit the F# project in notepad and reorder the source files which worked perfectly. Also, now with the latest F# builds for both VS 2008 and VS 2010, you can reorder directly in the IDE by right-clicking the file in the Solution Explorer and chosing "Move Up" or "Move Down"

By on 2/18/2010 3:18 PM ()

It'd be cool to have better (any?) code navigation features in VS for F#.
Are there any improvements on that front in VS2010?

By on 2/19/2010 6:21 AM ()

"Go To Definition" (F12 in the F# profile) has worked for a while, but that's the main code navigation feature. This is an area of tooling we'll definitely be looking to improve in a future release.

By on 2/19/2010 8:00 AM ()

That's exactly what I needed to know to continue!

NOTE: I had to close my solution and re-open it at one point after moving stuff around, to get the changes seen, but then all was fine.

By on 3/19/2008 7:29 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper