www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - variadic mixin - the right tool for the job?

reply =?iso-8859-1?Q?Robert_M._M=FCnch?= <robert.muench saphirion.com> writes:
Hi, can something like this (I borrowed the C pre-processor idea) be 
done with variadic mixins?

#define log(variadic-arg)  sys-log("%s:%s" + variadic-arg[0], __FILE__, 
__LINE__, variadic-arg[1..$]);

I read that mixins can only be used for declarations, and this here is 
a function call. So wondering, how something like this can be done.

-- 
Robert M. Münch
http://www.saphirion.com
smarter | better | faster
Mar 18 2015
parent reply Daniel =?UTF-8?B?S296w6Fr?= via Digitalmars-d-learn writes:
On Wed, 18 Mar 2015 15:35:03 +0100
"Robert M. Münch via Digitalmars-d-learn"
<digitalmars-d-learn puremagic.com> wrote:

 Hi, can something like this (I borrowed the C pre-processor idea) be 
 done with variadic mixins?
 
 #define log(variadic-arg)  sys-log("%s:%s" + variadic-arg[0],
 __FILE__, __LINE__, variadic-arg[1..$]);
 
 I read that mixins can only be used for declarations, and this here
 is a function call. So wondering, how something like this can be done.
 
You probably does not need mixins: module main; import core.vararg; void some_fun(char c, string file, int line, ...) { import std.stdio : writeln; writeln(c, file, line); } void log(string file = __FILE__, size_t line = __LINE__, T...) (T variadic_arg) { some_fun(variadic_arg[0], file, line, variadic_arg[1 .. $]); } void main(string[] args) { log('c', 'm', 10); }
Mar 18 2015
parent =?iso-8859-1?Q?Robert_M._M=FCnch?= <robert.muench saphirion.com> writes:
On 2015-03-18 15:27:03 +0000, Daniel Kozák via Digitalmars-d-learn said:

 You probably does not need mixins:
 
 void log(string file = __FILE__, size_t line = __LINE__, T...)
 (T variadic_arg) {
     some_fun(variadic_arg[0],  file, line, variadic_arg[1 .. $]);
 }
Hi, ha, forgot about default arguments. Great that this works and take the file & line number where the tempate function is used. Thanks. -- Robert M. Münch http://www.saphirion.com smarter | better | faster
Mar 19 2015