Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing types across F# .fsx files

Tags:

f#

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?

like image 464
James Crowley Avatar asked Jun 18 '14 12:06

James Crowley


People also ask

How do I share TypeScript types between client and server?

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.


2 Answers

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:

  1. refuse to load the file if conflicts with existing names arises (complaining that some stuff is already defined)
  2. put the names in FS_00xx namespace (so that they are actually different types from the ones you already loaded), eventually opening 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).

like image 128
pqnet Avatar answered Sep 28 '22 16:09

pqnet


[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.

like image 27
Tomas Petricek Avatar answered Sep 28 '22 15:09

Tomas Petricek