emileverh 28 Posted September 26 Hi guys! I want to use in Delphi 13 the NameOf() function to get the current function name ( "UploadCardParseResult" in example below). Is that possible?! procedure TdtmPeppolInvoices.UploadCardParseResult(const AJsonString: string; const AHxr: THXR); begin ShowMessage( NameOf(????) ); Share this post Link to post
Anders Melander 2127 Posted September 26 37 minutes ago, emileverh said: I want to use in Delphi 13 the NameOf() function to get the current function name That's not what NameOf does: https://docwiki.embarcadero.com/RADStudio/Florence/en/What's_New#NameOf_Instrict_Function In short, there's no function to get the current function name. You can search these fora for various hacks, and I'm sure someone will post suggestions, but I wouldn't recommend any of them. Share this post Link to post
Kazantsev Alexey 28 Posted September 26 1 hour ago, emileverh said: I want to use in Delphi 13 the NameOf() function to get the current function name ( "UploadCardParseResult" in example below). Is that possible?! procedure TdtmPeppolInvoices.UploadCardParseResult(const AJsonString: string; const AHxr: THXR); begin ShowMessage( NameOf(TdtmPeppolInvoices.UploadCardParseResult) ); Share this post Link to post
Uwe Raabe 2222 Posted September 26 With the introduction of NameOf there are still gaps where it doesn't work (partly huge ones). I guess we can expect extensions to NameOf targets in future updates and releases. 1 Share this post Link to post
GabrielMoraru 55 Posted Wednesday at 09:01 AM (edited) On 9/26/2025 at 1:47 PM, Anders Melander said: In short, there's no function to get the current function name. You can explicitly give the name of the current method. But I guess emileverh asks for something similar to "Self" ? I would love that. Something like: "CurrentMethod" 🙂 ShowMessage('Error in ' + CurrentMethod); That would have been useful on a project that truly abused Try/Except. Basically, everything was wrapped in Try/Except. The program never ever crashed! I wrote a small program to detect all empty try/except blocks and insert a log msg there (plus a counter). I was too lazy to detect the method name to put it in the message. ShowMessage('Swallowed exception in ' + CurrentMethod) would have been great 🙂 Edited Wednesday at 11:12 AM by GabrielMoraru 1 Share this post Link to post
Anders Melander 2127 Posted Wednesday at 10:31 AM (edited) 7 hours ago, GabrielMoraru said: You can explicitly give the name of the current method. Yes, but it's pointless. And I'm sure that won't stop people from doing it anyway. Like, I once worked with a developer who consistently used Pred and Succ on integers but couldn't explain why he didn't just use -1 and +1. I paid for the whole language, so I'm going to use the whole language. Next up: The ValueOf function: function ValueOf<T>(Value: T): T; begin Result := Value; end; Edited Wednesday at 04:12 PM by Anders Melander 3 Share this post Link to post
Angus Robertson 689 Posted Wednesday at 10:56 AM 22 minutes ago, Anders Melander said: I once worked with a developer who consistently used Pred and Succ on integers but couldn't explain why he didn't just use -1 and +1. I used to use Pred and Succ, mainly because early Intel 8080 compilers mapped those to simple assembly instructions, rather than using arithmetic. But compilers got better. Angus 1 Share this post Link to post
GabrielMoraru 55 Posted Wednesday at 11:05 AM 2 minutes ago, Angus Robertson said: I used to use Pred and Succ, mainly because early Intel 8080 compilers mapped those to simple assembly instructions, rather than using arithmetic. But compilers got better. And if you use it on constants, the result will be precalculated.. ____ > Yes, but it's pointless. Yeah. This is why I said that emileverh probalby wants something like "CurrentMethodName". I think NameOf came too late. Now I use madShi to see the call stack. ____ Succ(MaxInt) Share this post Link to post
Uwe Raabe 2222 Posted Wednesday at 11:49 AM The idea behind NameOf(<someIdentifier>) is to get a string representation of that identifier. If the underlying item of <someIdentifier> gets renamed the parameter to NameOf gets either magically changed too (using a capable rename refactoring) or the old identifier (hopefully) won't compile anymore. F.i. accessing RTTI information about a property using TRttiType.GetProperty requires the name of that property. Currently one needs to keep a string constant in sync with the property name. Unfortunately we have RSB-997 - NameOf() does not allow class members, so I still have to wait for that being implemented. I agree that not everyone wants to make use of NameOf, but is has been the most voted item in QualityPortal for quite some time. Probably there are a couple of users with a real need for that. Pointless doesn't quite hit it. Share this post Link to post
Anders Melander 2127 Posted Wednesday at 02:17 PM 1 hour ago, Uwe Raabe said: I agree that not everyone wants to make use of NameOf, but is has been the most voted item in QualityPortal for quite some time. Probably there are a couple of users with a real need for that. IMO the QC/QP votes are a useless metric as the votes are completely unqualified, the sample size is too small, and most people don't think about the consequences of their "choice". I seriously doubt that there are many that will have a use for it, but whatever; It probably took someone an hour to implement. I think what people really wanted was more what the OP asked for, but of course I can't know. Anyway, fair point about the refactoring/compile time check. 3 hours ago, GabrielMoraru said: And if you use it on constants, the result will be precalculated.. Um... It will anyway. Constant expressions are evaluated at compile time. Share this post Link to post
emileverh 28 Posted Thursday at 05:00 AM 19 hours ago, GabrielMoraru said: You can explicitly give the name of the current method. But I guess emileverh asks for something similar to "Self" ? I would love that. Something like: "CurrentMethod" 🙂 ShowMessage('Error in ' + CurrentMethod); That would have been useful on a project that truly abused Try/Except. Basically, everything was wrapped in Try/Except. The program never ever crashed! I wrote a small program to detect all empty try/except blocks and insert a log msg there (plus a counter). I was too lazy to detect the method name to put it in the message. ShowMessage('Swallowed exception in ' + CurrentMethod) would have been great 🙂 Exactly that is what I hope/hoped for 1 Share this post Link to post
Rollo62 616 Posted Thursday at 06:24 AM (edited) I cannot find any background information to NameOf(), how it internally really works. It seems to be implemented intrinsic in System.NameOf, but how does it magically retrieves a name at runtime? If this NameOf() call intrinsically also includes the RTTI-System by calling System.TypeInfo or the like, as an unwanted side-effect, that would not be a good thing, IMHO. Perhaps its some other kind of compiler magic, that is going on here. 🤔 Who knows what it really does, under the hood? Edit: Ok, I just checked what it can do and whats not. It really seems to replace only like: typing the text "nameof( Sender );" instead of "Sender"; typing the text "nameof( Self );" instead of "Self"; in the code. My gut feeling was right, yes its useless and even has more text to type. Or do I oversee some hidden useful magic or some really fancy use-cases here? Maybe the use in Attributes would make some sense https://en.delphipraxis.net/topic/1775-i-wish-i-had-rtti-for-constants-andor-a-compiler-magic-nameof/?do=findComment&comment=108278 Edited Thursday at 06:43 AM by Rollo62 1 Share this post Link to post
Uwe Raabe 2222 Posted Thursday at 07:32 AM 56 minutes ago, Rollo62 said: Or do I oversee some hidden useful magic or some really fancy use-cases here? There is a real use case mentioned in the https://github.com/UweRaabe/CmonLib/tree/main/Examples/Observers example of CmonLib. Currently the calls in Main.Form use constants to specify the property to observe: tmp.AddObserver<string>(tmp.cMyString, procedure(AValue: string) begin MyStringEdit.Text := AValue end); tmp.AddObserver<TStrings>(tmp.cMyLines, procedure(AValue: TStrings) begin MyLinesMemo.Lines := AValue end); tmp.AddObserver<Integer>(tmp.cMySelectedIndex, procedure(AValue: Integer) begin MySelectedComboBox.ItemIndex := AValue end); tmp.AddObserver<string>(tmp.cMySelected, procedure(AValue: string) begin MySelectedComboBox.Text := AValue end); tmp.AddObserver<Integer>(tmp.cMyListItemIndex, procedure(AValue: Integer) begin MyListItemListBox.ItemIndex := AValue end); The constants are declared like this (note the QP reference): type TObservableData = class(TData) public const { string representation of TData property names. I am eagerly waiting for the implementation of the top most voted feature request in QP: RSP-13290 "NameOf(T) compiler (magic) function" } cMyLines = 'MyLines'; cMyListItem = 'MyListItem'; cMyListItemIndex = 'MyListItemIndex'; cMySelected = 'MySelected'; cMySelectedIndex = 'MySelectedIndex'; cMyString = 'MyString'; With a proper NameOf the calls above could be written like this: tmp.AddObserver<string>(NameOf(tmp.MyString), procedure(AValue: string) begin MyStringEdit.Text := AValue end); The tricky part here is that NameOf(tmp.MyString) needs to return 'MyString' and not 'tmp.MyString'. (Perhaps I should file a use case for that. Otherwise I may have to wait a few more iterations.) 1 Share this post Link to post
GabrielMoraru 55 Posted Friday at 06:24 PM On 10/2/2025 at 8:24 AM, Rollo62 said: My gut feeling was right, yes its useless and even has more text to type. Actually, it has uses - limited but it has. It can be helpful when you change the name of an identifier (variable, method name, etc) at batch and your batch/refactor tool will not change it also in the string (in the error message): ShowMessage('Error in MyMethod'); With NameOf your code will not compile, so you will easily spot and replace the now outdated variable name: ShowMessage('Error in '+ NameOf(MyNewMethodName)); But the fact that it doesn't work on the name of the current method... it makes it even less useful.🙂 I wrote about this here. Actually I was praying for something like this in my early days of Delphi. Now are better ways... 1 Share this post Link to post
Rollo62 616 Posted Saturday at 08:28 AM Ok great, it forces you to change the name in all places, at least nicely supported by compiler and CodeCompletion, thats fair enough. 👍 1 Share this post Link to post