跨F#.fsx文件共享类型 [英] Sharing types across F# .fsx files

查看:78
本文介绍了跨F#.fsx文件共享类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在fsx文件之间共享类型?

Is there a way to share types across fsx files?

使用#load从多个FSX文件中加载包含类型的同一文件时,它们似乎每次都被前缀到不同的FS_00xx命名空间中,这意味着您无法传递它们.

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?

推荐答案

http://msdn.microsoft.com/en-us/library/dd233169.aspx

[.fsx文件]用于在F#中包含非正式测试代码,而无需将测试代码添加到您的应用程序中,也无需为其创建单独的项目.默认情况下,脚本文件不包含在项目的构建中,即使它们是项目的一部分也是如此.

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

这意味着,如果您的项目结构足以解决此类依赖性问题,则不应使用.fsx文件,而应使用.fs文件编写模块/命名空间.也就是说,您确实应该将它们的类型编译为程序集.

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.

f#交互式解释器为每个加载的文件生成程序集.如果两次加载文件,字节码将生成两次,并且即使它们具有相同的定义和名称,它们的类型也会不同.这意味着您无法在两个.fsx文件之间共享类型,除非其中一个包含另一个.

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.

#load文件的类型与环境中已经存在的文件类型相同时,f#交互式解释器可以使用两种不同的策略:

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. 如果与现有名称发生冲突(表示已经定义了某些内容),则拒绝加载文件
  2. 将名称放入FS_00xx命名空间(这样它们实际上就是与您已经加载的名称不同的类型),最终open生成结果命名空间,以便可以从交互式会话中使用这些名称.
  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.

由于应该将 fsx文件用作非正式测试,因此使用第二种方法更为用户友好(也存在使用第二种方法的技术原因,主要取决于.net VM类型系统,以及无法在运行时更改现有类型的事实.

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

这篇关于跨F#.fsx文件共享类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆