中的命名空间别名

中的命名空间别名

本文介绍了F#中的命名空间别名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用F#代码打开某些.Net程序集时,我遇到名称冲突的问题

I have a name clashing problem while opening some .Net assemblies in my F# code using

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.

是否有可能在F#中为.Net命名空间定义别名以防止名称冲突?

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. So, to resolve the conflicts between .NET assemblies, you will, unfortunatelly, need to define aliases for all the types you're using.

由于F#类型别名被视为普通类型声明(由F#编译器而非运行时),因此这可能会稍微容易一些.这意味着,与使用C#using关键字不同,您可以在spearate文件中定义它们:

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). This means that, unlike with C# using keyword, you can define them in a spearate file:

// 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

在应用程序的其余部分,您现在可以使用SysIO.File.您仍然必须编写别名,但至少不必在每个文件中都这样做...

In the rest of your application, you can now use SysIO.File. You still have to write the aliases, but at least you don't have to do that in every file...

这篇关于F#中的命名空间别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 05:03