#Where do we use inline ?

1 messages · Page 1 of 1 (latest)

alpine coyote
#

They do exist but i dont know where to use, iam confused. I read docs but couldnt figure it out

#

Where do we use inline ?

copper island
#

i've heard that you should almost never write inline fn in zig. here it is <#compiler-devel message>

#

is that what you mean? or are you talking about inline loops?

#

one time you need inline fns is when you want a comptime fn to be called without needing to write comptime foo(bar) so you define it like

inline fn foo(comptime bar: u8) { 
   comptime { ... } 
}
alpine coyote
#

iam talking about all inline

#

inline in switch cases, loops

#

functions

fierce osprey
#

inline in general means semantically “pasting” blocks of code to get better comptime compatibility.

inline for is required for looping over iterables that require a comptime index at runtime (you can loop over comptime only slices at comptime with a normal for loop), it pastes the loop body X amount of times where X is the length of the iterable.

inline switch cases paste a block for each prong, so inline .a, .b => |v| will generate two blocks where v is comptime known and is either .a or .b

inline while is pretty much the same as inline for, but with while semantics :P

#

there are times where inline can help with performance, but do not assume it will. always test whether it actually helps, otherwise youre actually hurting optimization because the optimizer has less choices when you inline things