Mixin operator 'if' directly
bauss
jj_1337 at live.dk
Wed Dec 19 15:12:14 UTC 2018
On Wednesday, 19 December 2018 at 15:09:47 UTC, bauss wrote:
> On Wednesday, 19 December 2018 at 13:37:17 UTC, Andrey wrote:
>> 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 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 >= index) // Reversed the logic.
> {
> // ... some code
> }
> }
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.
More information about the Digitalmars-d-learn
mailing list