Is there a way to share types across fsx files?
When using #load to load the same file containing a type from multiple FSX files they seem to be prefixed into a different FS_00xx namespace each time, which means you can't pass them around.
Are there any ways around this behaviour without resorting to compiling into an assembly?
As for
http://msdn.microsoft.com/en-us/library/dd233169.aspx
This means that if you have a project with enough structure to be having such dependency problems, you should not use
.fsx
files, instead write modules/namespaces using.fs
files. That is, you really should compile them types into an assembly.The f# interactive interpreter generates assembly for each loaded files. If you load a file twice, the bytecode is generated twice, and the types are different even if they have the same definition and the same name. This means that there is no way for you to share types between two .fsx files, unless one of them includes the other.
When you
#load
a file which has the same types as ones already present in your environment, the f# interactive interpreter can use two different strategy:FS_00xx
namespace (so that they are actually different types from the ones you already loaded), eventuallyopen
ing the resulting namespace so that names are available from interactive session.Since fsx files are supposed to be used as informal test it is more user-friendly to use the second approach (there are also technical reason for which the second approach is used, mainly dependent on
.net
VM type system, and the fact that existing types cannot be changed at runtime).[Note: This is a more specific answer to a more specific question that is a duplicate of this one.]
I don't think there is a nice and easy solution for this. The one solution I have been using in some projects (like the F# snippets web site) is to have only one top-level
fsx
file that loads a number offs
files. For example, see app.fsx.So, you would have
common.fs
,intMapper.fs
andstringMapper.fs
that would be loaded fromcaller.fsx
as follows:Inside
stringMapper.fs
andintMapper.fs
, you do not loadcommon.fs
. The common types will be loaded bycaller.fsx
before, so things will work.The only issue with this is that
intMapper.fs
now isn't a standalone script file - and if you want to get autocomplete in an editor, you need to add afsproj
file that specifies the file order. In F# snippets project, there is a project file which specifies the order in whch the editor should see and load the files.Have all the
#load
and#open
directives in the file you actually run from fsi.exe (C in the example below), and make sure the loaded files themselves do not#load
their own dependencies:Files A.fsx, B.fsx, C.fsx. B depends on A. C depends on B and A.
B contains
C contains
Unfortunately this then makes all the files hard to edit from Visual Studio - the editor doesn't know about their dependencies and shows all sorts of errors.
Therefore this is a bit of a hack, and the recommended way seems to be to have a single .fsx file and compile everything else into a .dll :