Why does stringof not like functions with arguments?

Chad Joan chadjoan at gmail.com
Fri Jul 31 09:38:16 UTC 2020


On Thursday, 10 August 2017 at 14:51:22 UTC, Meta wrote:
> On Wednesday, 9 August 2017 at 01:39:07 UTC, Jason Brady wrote:
>> Why does the following code error out with:
>>
>> app.d(12,10): Error: function app.FunctionWithArguments (uint 
>> i) is not callable using argument types ()
>>
>> Code:
>>
>> import std.stdio;
>>
>> void FunctionWithoutArguments() {
>> }
>>
>> void FunctionWithArguments(uint i) {
>> }
>>
>> void main()
>> {
>> 	writeln(FunctionWithoutArguments.stringof);
>> 	writeln(FunctionWithArguments.stringof);
>> }
>
> Welcome to optional parentheses hell. Please enjoy your stay.
> [...]

Muahahaha it's necromancy time!

... meaning I just ran into this problem. Again. And it sucked. 
And I found this thread. Again. Now it's time for me to be less 
of a dummy and post my solution.

This seems to have different solutions depending on what you want 
out of the function symbol. The advice already given in this 
thread is great if you want to print the function's name (and 
maybe a couple other things I already forgot).

But what I needed was to print the function's *signature*.

Basically, I want to

   writeln(FunctionWithArguments.stringof);

and get this output:

   void FunctionWithArguments(uint i)


I didn't quite get there. I got this far:

   void(uint i)

But for what I'm doing right now, that's good enough.


Alright here's how it's done:

   writeln(typeof(FunctionWithArguments).stringof);

So it was ultimately really easy. At least, for this one very 
specific use-case. I just about kicked myself.


The previous example then becomes this:

import std.stdio;

void FunctionWithoutArguments() {
}

void FunctionWithArguments(uint i) {
}

void main()
{
     writeln(typeof(FunctionWithoutArguments).stringof);
     writeln(typeof(FunctionWithArguments).stringof);
}

I needed this when writing a program that checks for whether 
functions visible from alias-this statements are included in the 
results of __traits(getOverloads,...).
Here is the end result:
https://pastebin.com/yj3idDhp

And no. No they are not. :3


More information about the Digitalmars-d-learn mailing list