dotnet / dotnet/vblang

Support creation of type aliases for delegate types via Imports statement.

Open
#350 2 comments 4 reactions 0 assignees View on GitHub
Dominant language
No language data
Stars
328
Forks
71
PR merge metrics
No merged PRs in 30d

Description

Discussion related to [#30724](https://github.com/dotnet/roslyn/issues/30724). Basically, this code should compile:

```vb
Imports MyFunction = System.Func(Of String) '<-- compiler doesn't like this.

Public Module Module1
Public Sub Main()

End Sub
End Module
```

...because the language spec [says that it should](https://docs.microsoft.com/en-us/dotnet/visual-basic/reference/language-specification/source-files-and-namespaces#import-aliases):

> An import alias can create an alias **for any namespace or type**. Accessing a namespace or type through an alias yields exactly the same result as accessing the namespace or type through its declared name

Creating type aliases is very useful when dealing with complex generic types (e.g a dictionary where the values are themselves dictionaries) so removing the above limitation would be a small but useful enhancement.

While on the topic, it would also be great to remove the limitation that says you can't refer to other type aliases when defining new aliases. For example this doesn't compile:

```vb
Imports StringList = System.Collections.Generic.List(Of String)
Imports StringListTable = System.Collections.Generic.Dictionary(Of String, StringList)
```

...because you can't refer to the `StringList` alias when defining the `StringListTable` alias. Combined with the rule that the types must be fully qualified, you end up having to type this keyboard killer:

```vb
Imports StringListTable = System.Collections.Generic.Dictionary(Of String, System.Collections.Generic.List(Of String))
```

In summary, type aliases can be made a great deal more useful by making the following changes:

1. Allow creation of type aliases for delegate types like `System.Func(Of T)`.
2. Allow creation of type aliases without using fully qualified type names when possible.
3. Allow creation of type aliases that refer to other type aliases defined in same file.
4. Allow creation of type aliases in scopes smaller than the file (e.g inside a single class/method).

Number 1 is the primary request in this issue, but if the language team is willing to look at the area as a whole, then features number 2 to 4 above would be great to include under the umbrella of **improved alias types** in a future version of VB.

CC: @KathleenDollard

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.