dotnet / dotnet/vblang

Define aliases for types and members using :=

Open
#442 8 comments 1 reaction 0 assignees View on GitHub
Dominant language
No language data
Stars
328
Forks
71
PR merge metrics
No merged PRs in 30d

Description

Define aliases for types and members using :=

Today, `Using` allow some limited form of aliasing. I suggest to use `:=` instead to allow class-level and local aliasing. For example, the following code is not allowed:
```VB.NET:
Dim Report = AddressOf Console.WriteLine
Report("test")
```

Because `Console.WriteLine` has many overloads, the `Report` var is inferred to be of type Object! Generally, type inference doesn't work with AddressOf!
We have to define a delegate for only one overload:
```VB.NET:
Dim Report As Action(Of String) = AddressOf Console.WriteLine
Report("test")
```

My suggestion. is to use an alias instead:
```VB.NET:
Dim Report := Console.WriteLine
Report("test")
Dim n = 1
Report("test {0}", n)
Dim GetFnwe := IO.Path.GetFileNameWithoutExtension
Report(GetFnwe("C:\1.txt"))
Dim Dic := Dictionary(Of Integer, List(Of String))
Dim D As New Dic()
D.Add(1, New List(Of String))
Dim student := (Id as Integer, Name As String, Grade As Integer)
Dim Adam As Student
Adam.Name = "Adam"
```

This can be combined with #438 to get:
```VB.NET:
With n = 1,
Report := Console.WriteLine,
GetFnwe := IO.Path.GetFileNameWithoutExtension,
Dic := Dictionary(Of Integer, List(Of String)),
D As New Dic(),
student := (Id as Integer, Name As String, Grade As Integer),
Adam As Student

Report("test")
Report("test {0}", n)
Report(GetFnwe("C:\1.txt"))
D.Add(1, New List(Of String))
Adam.Name = "Adam"
End With
```

And as I argued there, `=` should be used to assign scoped variables, while `:=` should be used for aliasing.
And of course defining `Dic` and `Student` at the class level will be better, to use them all over the class scope. This is just a quick example for demonstration .

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.