Thread overview
variadic mixin - the right tool for the job?
Mar 18, 2015
Robert M. Münch
Mar 18, 2015
Daniel Kozák
Mar 19, 2015
Robert M. Münch
March 18, 2015
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

March 18, 2015
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);
}

March 19, 2015
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