Here I am having a problem with templates again. No matter how much I read, I can't seem to understand how templates/mixins work. So I'm having the following code (just a snippet of the real code):
if (c != '%') {
if (stdout_index < STDOUT_BUF_LEN) {
stdout_buffer[stdout_index++] = c;
continue;
} else {
sys_write(1, stdout_buffer.ptr, cast(i32)stdout_index);
stdout_index = 0;
stdout_buffer[stdout_index++] = c;
continue;
}
}
And I want to create a macro (using the C terms) to make the code inside the first if statement (if (c != '%')
) into a template that will be able to used and added in place (not as a function as I don't want to function call). I tried to make it both a template and a mixin template and It will not compile, rather it will give my the following error:
Error: declaration expected, not `if`
Error: declaration expected, not `continue`
Error: declaration expected, not `else`
Error: basic type expected, not `0`
Error: found `0` when expecting `;`
Error: no identifier for declarator `stdout_buffer[stdout_index++]`
Error: declaration expected, not `=`
Error: declaration expected, not `continue`
Error: unrecognized declaration
It should be clear what I tried to still I will post what I tried in case someone is curious to see:
mixin template add_char() {
if (stdout_index < STDOUT_BUF_LEN) {
stdout_buffer[stdout_index++] = c;
continue;
} else {
sys_write(1, stdout_buffer.ptr, cast(i32)stdout_index);
stdout_index = 0;
stdout_buffer[stdout_index++] = c;
continue;
}
}
So any ideas why this doesn't work?