Thread overview | |||||
---|---|---|---|---|---|
|
March 18, 2015 variadic mixin - the right tool for the job? | ||||
---|---|---|---|---|
| ||||
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 Re: variadic mixin - the right tool for the job? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Robert M. Münch | 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 Re: variadic mixin - the right tool for the job? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Kozák | 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 |
Copyright © 1999-2021 by the D Language Foundation