One of us is crazy: Me or {function here}.stringof
Nick Sabalausky
a at a.a
Thu Nov 12 23:53:58 PST 2009
AKA ".stringof strikes again", or ".attackof.stringof"...
Not sure if this is right or not:
------------------------------
void foo(){}
pragma(msg, foo.stringof);
------------------------------
Outputs "foo()", but shouldn't it just be "foo" instead? Or am I overlooking
something?
And on top of that, giving foo a parameter:
------------------------------
void foo(int i){}
pragma(msg, foo.stringof);
------------------------------
Error: function main.foo (int) does not match parameter types ()
Error: expected 1 arguments, not 0
WTF?
Ok, so maybe that stupid optional-parens function-invocation "feature" is
kicking in where it's not wanted. D's standard way to refer to a function
itself is supposed to be '&':
------------------------------
void foo(int i){}
pragma(msg, &foo.stringof);
------------------------------
main.d(2): Error: function main.foo (int) does not match parameter types ()
main.d(2): Error: expected 1 arguments, not 0
main.d(2): Error: "foo()"c is not an lvalue
main.d(2): Error: pragma msg string expected for message, not '&"foo()"c'
FFPPJTTdD!!!!!
Associativity problem?
------------------------------
void foo(int i){}
pragma(msg, (&foo).stringof);
------------------------------
& foo
Argh! (Not to be confused with "Args!")
Sooooooooo.......
If I'm writing a template that takes in a varadic list of variables and
functions, and does something with their names, what's the right way to do
that (if any)? Trivial example:
------------------------------
template makeBools(idents...)
{
const char[] foo = "bool _generated_from_"~idents[0].stringof~"_name;" ~
foo!(idents[1..$]);
}
int i;
void func1(){}
void func2(int x){}
mixin(makeBools!(i, func1, func2)); // Thoroughly fucks up.
------------------------------
I suppose I could resort to passing in string literals, but I'd really
rather not have to.
More information about the Digitalmars-d-learn
mailing list