简体   繁体   English

F#中的命名空间别名?

[英]Namespace aliasing in F#?

I have a name clashing problem while opening some .Net assemblies in my F# code using 使用时,我在F#代码中打开一些.Net程序集时会出现名称冲突问题

open System.IO

The only option I found and use now is to provide the full names of types for conflicting types, but this is a little bit boring. 我现在找到并使用的唯一选项是为冲突类型提供类型的全名,但这有点无聊。

Is there any possibility to define an alias for .Net namespace in F# to prevent name clashing? 是否有可能在F#中为.Net命名空间定义别名以防止名称冲突?

F# does not support aliasing of namespaces - only modules and types. F#不支持名称空间的别名 - 只支持模块和类型。 So, to resolve the conflicts between .NET assemblies, you will, unfortunatelly, need to define aliases for all the types you're using. 因此,要解决.NET程序集之间的冲突,您将不幸地需要为您正在使用的所有类型定义别名。

This may be slightly easier thanks to the fact that F# type aliases are viewed as normal type declarations (by the F# compiler, not by the runtime). 由于F#类型别名被视为普通类型声明(由F#编译器,而不是运行时),因此这可能会稍微容易一些。 This means that, unlike with C# using keyword, you can define them in a spearate file: 这意味着,与C# using keyword不同,您可以在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

In the rest of your application, you can now use SysIO.File . 在应用程序的其余部分中,您现在可以使用SysIO.File You still have to write the aliases, but at least you don't have to do that in every file... 您仍然必须编写别名,但至少您不必在每个文件中都这样做...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM