Thread overview
Reflection
Feb 28, 2012
Joshua Niehus
Feb 28, 2012
Jesse Phillips
Feb 28, 2012
Puming
Feb 28, 2012
Jesse Phillips
Feb 28, 2012
Joshua Niehus
Feb 28, 2012
James Miller
Feb 28, 2012
Timon Gehr
Feb 28, 2012
Jonathan M Davis
February 28, 2012
Hello,

I dont understand the following snippet's output:

import std.stdio, std.traits;
void main() {
    writeln(isSomeFunction!(writeln));
    writeln(isCallable!(writeln));
    writeln("Yes I am...");
}

/* OUTPUT */
false
false
Yes I am...

If 'writeln' isn't a method/function and it's not callable, then what is it?

Thanks,
Josh
February 28, 2012
On Tuesday, 28 February 2012 at 05:56:12 UTC, Joshua Niehus wrote:
> Hello,
>
> I dont understand the following snippet's output:
>
> import std.stdio, std.traits;
> void main() {
>     writeln(isSomeFunction!(writeln));
>     writeln(isCallable!(writeln));
>     writeln("Yes I am...");
> }
>
> /* OUTPUT */
> false
> false
> Yes I am...
>
> If 'writeln' isn't a method/function and it's not callable, then what is it?
>
> Thanks,
> Josh

It is a template.
February 28, 2012
> It is a template.

Is a template callable? So what's the isCallable and isSomeFunction equivalent for templates?
February 28, 2012
On Tuesday, 28 February 2012 at 06:10:11 UTC, Jesse Phillips wrote:
> It is a template.

I see, thanks.
And I bet its not possible to figure out if a template is a "function template" or a "class template" etc...


February 28, 2012
On 28 February 2012 19:27, Joshua Niehus <jm.niehus@gmail.com> wrote:
> On Tuesday, 28 February 2012 at 06:10:11 UTC, Jesse Phillips wrote:
>>
>> It is a template.
>
>
> I see, thanks.
> And I bet its not possible to figure out if a template is a "function
> template" or a "class template" etc...
>

Not really, there might be, but not an obvious one. Its because templates can hold multiple types of declarations, not just functions or classes. The "eponymous template" pattern you see normally is not mandatory.

--
James Miller
February 28, 2012
On Tuesday, 28 February 2012 at 06:26:38 UTC, Puming wrote:
>> It is a template.
>
> Is a template callable? So what's the isCallable and isSomeFunction equivalent for templates?

I suppose it could be considered a bug as it is mostly a syntactic request/check. I'd expect

assert(isCallable!(writeln!string))

to be true. But as it is callable without explicit instantiation...
February 28, 2012
On 02/28/2012 07:27 AM, Joshua Niehus wrote:
> On Tuesday, 28 February 2012 at 06:10:11 UTC, Jesse Phillips wrote:
>> It is a template.
>
> I see, thanks.
> And I bet its not possible to figure out if a template is a "function
> template" or a "class template" etc...
>
>

You can trivially test whether the symbol is callable with some specific arguments, if that helps:

is(typeof(writeln(arg1,arg2,arg3)))
February 28, 2012
On Tuesday, February 28, 2012 06:56:10 Joshua Niehus wrote:
> Hello,
> 
> I dont understand the following snippet's output:
> 
> import std.stdio, std.traits;
> void main() {
> writeln(isSomeFunction!(writeln));
> writeln(isCallable!(writeln));
> writeln("Yes I am...");
> }
> 
> /* OUTPUT */
> false
> false
> Yes I am...
> 
> If 'writeln' isn't a method/function and it's not callable, then what is it?

writeln doesn't even really exist as far as the compiler is concerned until it's been instantiated. So, testing writeln isn't going to work. You need to either test a specific instantiation (e.g. isSomeFunction!(writeln!string))), or you need to just test that particular call works (e.g. is(typeof(writeln(arg)))). The second is probably better, since that's generally what you really care about - is it compilable with the given set of arguments.

- Jonathan M Davis