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:
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:
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!
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. 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:
#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!
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.
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.
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"
It'd be cool to have better (any?) code navigation features in VS for F#.
Are there any improvements on that front in VS2010?
Are there any improvements on that front in VS2010?
"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.
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 listDoesn't alt-[up or down arrow] when the file is selected in the Solution Explorer reorder in Visual Studio 2010 rc?
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. :)
Topic tags
- f# × 3583
- compiler × 262
- functional × 199
- c# × 117
- classes × 96
- web × 94
- book × 83
- .net × 80
- websharper × 72
- async × 71
- server × 43
- parallel × 42
- parsing × 41
- testing × 41
- asynchronous × 29
- monad × 28
- ocaml × 26
- tutorial × 26
- haskell × 25
- workflows × 21
- html × 20
- introduction × 19
- linq × 19
- wpf × 19
- silverlight × 18
- collections × 14
- pipeline × 14
- fpish × 13
- templates × 12
- monads × 11
- opinion × 10
- plugin × 9
- reactive × 9
- scheme × 9
- solid × 9
- basics × 8
- concurrent × 8
- deployment × 8
- how-to × 8
- sitelets × 8
- complexity × 7
- python × 7
- javascript × 6
- lisp × 6
- real-world × 6
- workshop × 6
- xaml × 6
- conference × 5
- dsl × 5
- java × 5
- ml × 5
- scala × 5
- jquery × 4
- lift × 4
- metaprogramming × 4
- teaching × 4
- alt.net × 3
- aml × 3
- enhancement × 3
- formlets × 3
- reflection × 3
- compilation × 2
- computation expressions × 2
- corporate × 2
- cufp × 2
- enterprise × 2
- erlang × 2
- http × 2
- interactive × 2
- interface × 2
- iphone × 2
- iteratee × 2
- jobs × 2
- keynote × 2
- numeric × 2
- obfuscation × 2
- oop × 2
- packaging × 2
- pipelines × 2
- sockets × 2
- stm × 2
- tcp × 2
- type provider × 2
- visual studio × 2
- .net interop × 1
- abstract class × 1
- agents × 1
- agile × 1
- appcelerator × 1
- asp.net mvc 4 × 1
- asp.net web api × 1
- ast × 1
- b-tree × 1
- bistro × 1
- blog × 1
- bug × 1
- client × 1
- cloud × 1
- continuation-passing style × 1
- css × 1
- data × 1
- database × 1
- declarative × 1
- dhtmlx × 1
- documentation × 1
- dol × 1
- domain × 1
- eclipse × 1
- em algorithm × 1
- emacs × 1
- emotion × 1
- error × 1
- example × 1
- extension methods × 1
- fear × 1
- fp × 1
- functional style × 1
- gc × 1
- generic × 1
- geometry × 1
- getlastwin32error × 1
- hash × 1
- history × 1
- html5 × 1
- httpcontext × 1
- hubfs × 1
- installer × 1
- interpreter × 1
- io × 1
- ios × 1
- ipad × 1
- kendo × 1
- licensing × 1
- list × 1
- macro × 1
- macros × 1
- marshal × 1
- math × 1
- metro style × 1
- micro orm × 1
- minimum-requirements × 1
- multithreading × 1
- nancy × 1
- nested loops × 1
- object relation mapper × 1
- object-oriented × 1
- offline × 1
- option × 1
- orm × 1
- osx × 1
- pattern matching × 1
- performance × 1
- phonegap × 1
- programming × 1
- quant × 1
- range × 1
- raphael × 1
- real-time × 1
- restful × 1
- round table × 1
- runtime × 1
- rx × 1
- script × 1
- session-state × 1
- sitelet × 1
- sql × 1
- stickynotes × 1
- stress × 1
- structures × 1
- tdd × 1
- trie × 1
- type × 1
- type providers × 1
- upload × 1
- vector × 1
- visual f# × 1
- visual studio shell × 1
- windows-phone × 1
- winrt × 1
- xna × 1
![]() |
Copyright (c) 2011-2012 IntelliFactory. All rights reserved. Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Built with WebSharper |


What is the simplest way to split my growing F# project into multiple files?
Do I need to do something similar, and wrap all the stuff in each file inside a class or module declaration?
I tried naively doing "open foo", where I have a file "foo.fs"...
Or do I have to manually create ".fsi" files? That would be lame, so I hope not.
Oh, I haven't tried putting "public" on any declarations like in C# .. does that help?
Anyway, I could use a good example, tutorial, etc.
~TMSteve