Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Namespace aliasing in F#?

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.

Is there any possibility to define an alias for .Net namespace in F# to prevent name clashing?

like image 970
Alexander Galkin Avatar asked Feb 21 '12 14:02

Alexander Galkin


People also ask

What is a namespace can we create alias of a namespace?

Namespaces in C# serve two main purposes: to organize classes and functionality and to control the scope of classes and methods. Type aliasing is a little known feature for C# and it comes in handy when you would like to alias a specific method or class from a namespace for the sake of clarity.

How will you alias a namespace write an example?

Namespace Alias Qualifier(::) makes the use of alias name in place of longer namespace and it provides a way to avoid ambiguous definitions of the classes. It is always positioned between two identifiers. The qualifier looks like two colons(::) with an alias name and the class name. It can be global.

What is a namespace alias?

Namespace aliases allow the programmer to define an alternate name for a namespace. They are commonly used as a convenient shortcut for long or deeply-nested namespaces.


1 Answers

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.

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 

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

like image 54
Tomas Petricek Avatar answered Sep 22 '22 07:09

Tomas Petricek