dotnet / vblang

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

[Proposal] NameOf(Me) #20

Open AdamSpeight2008 opened 7 years ago

AdamSpeight2008 commented 7 years ago

Ported from Roslyn Repo

NameOf(Me) should evaluate to the Class / Structure / Module name, it is used in.

Module Exts
  Public Function Foo() As Object
    Debug.WriteLine(NameOf(Me)) ' output "Exs"
 End Function
End Module

Advantage Doesn't require reflection. Doesn't require repetition of the TypeName

jnm2 commented 7 years ago

Unless Me.Foo() is valid, NameOf(Me) would be inconsistent.

jrmoreno1 commented 7 years ago

There is (or was) a more general request that would cover the current method/property. Solving it for just the class/module is just too limiting.

AnthonyDGreen commented 7 years ago

Why is repeating the type name an issue here?

KlausLoeffelmann commented 7 years ago

I have a couple of questions.

What happens, if NameOf(Me) is used in a Method in Foo, and called somewhere from Bar, and that Method gets inlined? Reflection would return something different than NameOf, right? I would not like that.

And would you expect that NameOf(Me) is returning different results in derived classes? So, if you have

Class Foo Function FooMethod as String Return NameOf(Me) End Fuction End Class

Class Bar Inherits Foo End Class

What happens if you call Foo.FooMethod in contrast to Bar.FooMethod? Remember: the Return-Value is prepared already at Compile-Time!

jrmoreno1 commented 7 years ago

@AnthonyDGreen: I read "Doesn't require repetition of the TypeName" to mean "doesn't require magic strings".

AnthonyDGreen commented 7 years ago

@jrmoreno1 you don't need magic strings, you can just say NameOf(TypeName)

jrmoreno1 commented 7 years ago

@AnthonyDGreen : yep, you're right. Can't remember what I was thinking, but what I said is obviously wrong.