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?
Stages: (1) Copy client and server code, install client dependencies and build client. (2) Start afresh, copy server code only, install server dependencies and build server.
As for
http://msdn.microsoft.com/en-us/library/dd233169.aspx
[.fsx files are] used to include informal testing code in F# without adding the test code to your application, and without creating a separate project for it. By default, script files are not included in the build of a project even when they are part of a project.
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), eventually open
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 of fs
files. For example, see app.fsx.
So, you would have common.fs
, intMapper.fs
and stringMapper.fs
that would be loaded from caller.fsx
as follows:
#load "common.fs"
#load "stringMapper.fs"
#load "intMapper.fs"
open Common
Inside stringMapper.fs
and intMapper.fs
, you do not load common.fs
. The common types will be loaded by caller.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 a fsproj
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With