Ale*_*kin 21 f# alias namespaces
使用时,我在F#代码中打开一些.Net程序集时会出现名称冲突问题
open System.IO
Run Code Online (Sandbox Code Playgroud)
我现在找到并使用的唯一选项是为冲突类型提供类型的全名,但这有点无聊.
是否有可能在F#中为.Net命名空间定义别名以防止名称冲突?
Tom*_*cek 38
F#不支持名称空间的别名 - 只支持模块和类型.因此,要解决.NET程序集之间的冲突,您将不幸地需要为您正在使用的所有类型定义别名.
由于F#类型别名被视为普通类型声明(由F#编译器,而不是运行时),因此这可能会稍微容易一些.这意味着,与C#using关键字不同,您可以在spearate文件中定义它们:
// Aliases.fs
namespace SysIO
// Open the 'System' namespace to get a bit shorter syntax
// ('type File = File' is interpreted as discriminated union)
open System
type File = IO.File
type Directory = IO.Directory
Run Code Online (Sandbox Code Playgroud)
在您的应用程序的其余部分,您现在可以使用SysIO.File.您仍然必须编写别名,但至少您不必在每个文件中都这样做...