digitalmars.D.learn - DRY version of `static if(__traits(compiles, expr)) fun(expr)`
- Timothee Cour via Digitalmars-d-learn (13/13) Dec 13 2016 what's the best (and DRY) way to achieve:
- Timon Gehr (5/18) Dec 13 2016 I usually do
- Basile B. (15/40) Dec 14 2016 Strangely if i put this in a templated enum that doesn't work.
- =?UTF-8?Q?Ali_=c3=87ehreli?= (6/16) Dec 14 2016 When you do that, the code does not match the syntax of
- Basile B. (5/26) Dec 14 2016 I see, it makes sense. Anyway the two templates have a common
- Timon Gehr (5/34) Dec 15 2016 Shortcut that works:
what's the best (and DRY) way to achieve: ``` static if(__traits(compiles, expr)) fun(expr); ``` ie, without repeating the expression inside expr? eg: ``` static if(__traits(compiles, foo.bar[2])){ counter++; writeln(" expr = ", foo.bar[2]); } ```
Dec 13 2016
On 14.12.2016 00:00, Timothee Cour via Digitalmars-d-learn wrote:what's the best (and DRY) way to achieve: ``` static if(__traits(compiles, expr)) fun(expr); ``` ie, without repeating the expression inside expr? eg: ``` static if(__traits(compiles, foo.bar[2])){ counter++; writeln(" expr = ", foo.bar[2]); } ```I usually do enum code = q{expr}; static if(__traits(compiles,mixin(code))) fun(mixin(code));
Dec 13 2016
On Tuesday, 13 December 2016 at 23:37:59 UTC, Timon Gehr wrote:On 14.12.2016 00:00, Timothee Cour via Digitalmars-d-learn wrote:Strangely if i put this in a templated enum that doesn't work. If instead i use a delegate literal it works. enum Compiles(string code) = is(typeof((){mixin(code);})); enum Compiles2(string code) = __traits(compiles, mixin(code)); unittest { static assert(Compiles!q{auto a = 1+1;}); static assert(!Compiles!q{1;}); static assert(!Compiles!q{a = 1;}); static assert(Compiles2!q{auto a = 1+1;}); static assert(!Compiles2!q{1;}); static assert(!Compiles2!q{a = 1;}); } wtf ?what's the best (and DRY) way to achieve: ``` static if(__traits(compiles, expr)) fun(expr); ``` ie, without repeating the expression inside expr? eg: ``` static if(__traits(compiles, foo.bar[2])){ counter++; writeln(" expr = ", foo.bar[2]); } ```I usually do enum code = q{expr}; static if(__traits(compiles,mixin(code))) fun(mixin(code));
Dec 14 2016
On 12/14/2016 09:25 AM, Basile B. wrote:On Tuesday, 13 December 2016 at 23:37:59 UTC, Timon Gehr wrote:When you do that, the code does not match the syntax of __traits(compiles). Putting the code inside a scope works at least in this case: enum Compiles2(string code) = __traits(compiles, mixin('{' ~ code ~ '}')); AliI usually do enum code = q{expr}; static if(__traits(compiles,mixin(code))) fun(mixin(code));Strangely if i put this in a templated enum that doesn't work. If instead i use a delegate literal it works. enum Compiles(string code) = is(typeof((){mixin(code);})); enum Compiles2(string code) = __traits(compiles, mixin(code));
Dec 14 2016
On Wednesday, 14 December 2016 at 22:06:35 UTC, Ali Çehreli wrote:On 12/14/2016 09:25 AM, Basile B. wrote:I see, it makes sense. Anyway the two templates have a common problem (protection attributes: data used in the code must be visible to the outside), so using them as a shortcut is a false good idea.On Tuesday, 13 December 2016 at 23:37:59 UTC, Timon Gehrwrote:When you do that, the code does not match the syntax of __traits(compiles). Putting the code inside a scope works at least in this case: enum Compiles2(string code) = __traits(compiles, mixin('{' ~ code ~ '}')); AliI usually do enum code = q{expr}; static if(__traits(compiles,mixin(code))) fun(mixin(code));Strangely if i put this in a templated enum that doesn't work. If instead i use a delegate literal it works. enum Compiles(string code) = is(typeof((){mixin(code);})); enum Compiles2(string code) = __traits(compiles, mixin(code));
Dec 14 2016
On 15.12.2016 01:38, Basile B. wrote:On Wednesday, 14 December 2016 at 22:06:35 UTC, Ali Çehreli wrote:Shortcut that works: enum ifCompiles(string code)=`static if(__traits(compiles,{ `~code~` })){ `~code~` }`; mixin(ifCompiles!q{ ... });On 12/14/2016 09:25 AM, Basile B. wrote:I see, it makes sense. Anyway the two templates have a common problem (protection attributes: data used in the code must be visible to the outside), so using them as a shortcut is a false good idea.On Tuesday, 13 December 2016 at 23:37:59 UTC, Timon Gehrwrote:When you do that, the code does not match the syntax of __traits(compiles). Putting the code inside a scope works at least in this case: enum Compiles2(string code) = __traits(compiles, mixin('{' ~ code ~ '}')); AliI usually do enum code = q{expr}; static if(__traits(compiles,mixin(code))) fun(mixin(code));Strangely if i put this in a templated enum that doesn't work. If instead i use a delegate literal it works. enum Compiles(string code) = is(typeof((){mixin(code);})); enum Compiles2(string code) = __traits(compiles, mixin(code));
Dec 15 2016