Hello,
I am trying to understand this example in the documentation about inline functions:
Why does that test fails if you remove the inline
keyword? Why wouldn’t foo(1200, 34)
be equals to 1234
without the inline??
I guess my secondary question is: without the inline, foo(1200, 34)
equals to what at compile time?
I realized that the code works without the inline if I add comptime
just before the foo(...)
call, so I guess I can think of inline as a way to automatically add comptime before every call to that function, right?
It equals a value that is unknown until runtime. Because the compiler doesn’t know if the branch will be taken or not, it has to generate the code for both branches. In order to do that, it has to look at the code in both branches, and it will see the @compileError
inside the true branch. Whenever the compiler sees a @compileError
, it stops.
Another way to see this is doing this:
const x: i32 = 1234;
@compileLog(x);
//output: i32, 1234
@compileLog(foo(1200, 34));
//output: i32, runtime_value
Yes. Bear in mind that sometimes, in generic code, you may not be able to put the comptime
keyword at the call site, because it wouldn’t work for all types, so using inline
at the function declaration may allow some optimizations in this case.
5 Likes
I see, makes sense, thanks for pointing to @compileLog
, that helps!
Obrigado Lucas!
1 Like