Re: Mixin operator 'if' directly

2018-12-22 Thread Basile B. via Digitalmars-d-learn
On Saturday, 22 December 2018 at 10:11:23 UTC, bauss wrote: On Saturday, 22 December 2018 at 03:44:09 UTC, Timoses wrote: Awesome hack! Being a hack, it would be even nicer if it worked ouf of the box: mixin template foo(bool b) { int _impl() { writeln(b); return int.init; }

Re: Mixin operator 'if' directly

2018-12-22 Thread bauss via Digitalmars-d-learn
On Saturday, 22 December 2018 at 03:44:09 UTC, Timoses wrote: Awesome hack! Being a hack, it would be even nicer if it worked ouf of the box: mixin template foo(bool b) { int _impl() { writeln(b); return int.init; } int _ipml2 = _impl(); } vs mixin template f

Re: Mixin operator 'if' directly

2018-12-22 Thread Nicholas Wilson via Digitalmars-d-learn
On Saturday, 22 December 2018 at 03:44:09 UTC, Timoses wrote: On Wednesday, 19 December 2018 at 15:40:50 UTC, Neia Neutuladh wrote: On Wed, 19 Dec 2018 15:12:14 +, bauss wrote: Or while instantiating it: mixin template foo() { int _ignoreme() { if (readln.strip == "abort") throw new

Re: Mixin operator 'if' directly

2018-12-21 Thread Michelle Long via Digitalmars-d-learn
On Saturday, 22 December 2018 at 03:44:09 UTC, Timoses wrote: On Wednesday, 19 December 2018 at 15:40:50 UTC, Neia Neutuladh wrote: On Wed, 19 Dec 2018 15:12:14 +, bauss wrote: Or while instantiating it: mixin template foo() { int _ignoreme() { if (readln.strip == "abort") throw new

Re: Mixin operator 'if' directly

2018-12-21 Thread Timoses via Digitalmars-d-learn
On Wednesday, 19 December 2018 at 15:40:50 UTC, Neia Neutuladh wrote: On Wed, 19 Dec 2018 15:12:14 +, bauss wrote: Or while instantiating it: mixin template foo() { int _ignoreme() { if (readln.strip == "abort") throw new AbortException; return 1; } int _alsoIgnoreMe = _ignor

Re: Mixin operator 'if' directly

2018-12-21 Thread Michelle Long via Digitalmars-d-learn
On Thursday, 20 December 2018 at 16:23:39 UTC, H. S. Teoh wrote: On Thu, Dec 20, 2018 at 11:04:19AM +, bauss via Digitalmars-d-learn wrote: On Wednesday, 19 December 2018 at 15:40:50 UTC, Neia Neutuladh wrote: [...] > mixin template foo() > { > int _ignoreme() > { > if (readln.stri

Re: Mixin operator 'if' directly

2018-12-20 Thread bauss via Digitalmars-d-learn
On Thursday, 20 December 2018 at 16:23:39 UTC, H. S. Teoh wrote: On Thu, Dec 20, 2018 at 11:04:19AM +, bauss via Digitalmars-d-learn wrote: [...] [...] [...] Me too! This is awesome! This basically lets you insert arbitrary code via mixin templates with essentially no restrictions!

Re: Mixin operator 'if' directly

2018-12-20 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Dec 20, 2018 at 11:04:19AM +, bauss via Digitalmars-d-learn wrote: > On Wednesday, 19 December 2018 at 15:40:50 UTC, Neia Neutuladh wrote: [...] > > mixin template foo() > > { > > int _ignoreme() > > { > > if (readln.strip == "abort") throw new AbortException; > > return 1;

Re: Mixin operator 'if' directly

2018-12-20 Thread bauss via Digitalmars-d-learn
On Wednesday, 19 December 2018 at 15:40:50 UTC, Neia Neutuladh wrote: On Wed, 19 Dec 2018 15:12:14 +, bauss wrote: That's assuming that it's compile-time data though. If not then you can't do what you want to do. What you can do is wrap it in a function in the mixin template which you jus

Re: Mixin operator 'if' directly

2018-12-19 Thread Neia Neutuladh via Digitalmars-d-learn
On Wed, 19 Dec 2018 15:12:14 +, bauss wrote: > That's assuming that it's compile-time data though. > > If not then you can't do what you want to do. > > What you can do is wrap it in a function in the mixin template which you > just call after instantiating it. Or while instantiating it: mi

Re: Mixin operator 'if' directly

2018-12-19 Thread bauss via Digitalmars-d-learn
to compile it, I get: Error: declaration expected, not if Is it possible to mixin operator 'if' directly inside my template mixin? What you want to use is "static if". The correct way to do the above would be this: mixin template create(alias input, uint index, alia

Re: Mixin operator 'if' directly

2018-12-19 Thread bauss via Digitalmars-d-learn
it possible to mixin operator 'if' directly inside my template mixin? What you want to use is "static if". The correct way to do the above would be this: mixin template create(alias input, uint index, alias data) { static if(input.length >= in

Re: Mixin operator 'if' directly

2018-12-19 Thread rikki cattermole via Digitalmars-d-learn
Mixin templates don't work on statements only declarations like structs.

Mixin operator 'if' directly

2018-12-19 Thread Andrey via Digitalmars-d-learn
Hi, Here is a template mixin: mixin template create(alias input, uint index, alias data) { if(input.length < index) return; // ... some code } When I try to compile it, I get: Error: declaration expected, not if Is it possible to mixin operator 'if' directly insid