dotnet / vblang

The home for design of the Visual Basic .NET programming language and runtime library.
290 stars 66 forks source link

Use NameOf with open generic types #122

Open craigajohnson opened 7 years ago

craigajohnson commented 7 years ago

I have a project which invokes late-bound calls on various methods/properties of a type inheriting from a generic base class. To guard against a breaking refactor, I use NameOf. This is all fine and good except I must specify a dummy class name in order to use NameOf.

Example:

    Sub Main()
        'workaround - no inherent value in having to specify type
        Console.WriteLine(NameOf(Grover(Of Object).Lichen))

        'preferable
        Console.WriteLine(NameOf(Grover(Of ).Lichen))
    End Sub

    Public Class Grover(Of T)
        Public Function Lichen() As Integer
            Return 23
        End Function
    End Class

This change would result in a nice code cleanup. I imagine it could be a gnarly change for a relatively unusual case though!

AnthonyDGreen commented 7 years ago

Love it!

AnthonyDGreen commented 7 years ago

I think we have more latitude in VB than in C# because NameOf is a proper keyword (like GetType) so we wouldn't need to make as much of the binder resilient to unexpected open generic types. I wonder if we can just substitute Object or Integer for the type arguments behind the scenes to simplify the implementation (would the hack bleed through to the IDE?). Certainly that approach could be taken to implement a prototype.

AnthonyDGreen commented 7 years ago

C# issue here.

alrz commented 7 years ago

NameOf is a proper keyword

wait, when that happened? could it break existing code?

AnthonyDGreen commented 7 years ago

NameOf was a proper keyword in VB from the start. We considered the risk, spoke with community members (MVPs specifically) and made that design decision. It was based on several factors including: