www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Reflection

reply "Joshua Niehus" <jm.niehus gmail.com> writes:
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
Feb 27 2012
next sibling parent reply "Jesse Phillips" <jessekphillips+D gmail.com> writes:
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.
Feb 27 2012
parent Timon Gehr <timon.gehr gmx.ch> writes:
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)))
Feb 27 2012
prev sibling next sibling parent "Puming" <zhaopuming gmail.com> writes:
 It is a template.

Is a template callable? So what's the isCallable and isSomeFunction equivalent for templates?
Feb 27 2012
prev sibling next sibling parent "Joshua Niehus" <jm.niehus gmail.com> writes:
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...
Feb 27 2012
prev sibling next sibling parent James Miller <james aatch.net> writes:
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
Feb 27 2012
prev sibling next sibling parent "Jesse Phillips" <jessekphillips+D gmail.com> writes:
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...
Feb 27 2012
prev sibling parent "Jonathan M Davis" <jmdavisProg gmx.com> writes:
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
Feb 28 2012