Find limit of recursion: Difference between revisions

Content added Content deleted
m (→‎{{header|Zig}}: Add third example where compiler automatically decides to do (or not to do) STRE (like it works with many other languages here))
m (→‎{{header|Zig}}: Note for 0.10.x versions)
Line 3,391: Line 3,391:
{{Trans|C}}
{{Trans|C}}


===Leave TRE to the compiler===
'''Works with:''' 0.11.x, 0.12.0-dev.1381+61861ef39
'''Works with:''' 0.11.x, 0.12.0-dev.1381+61861ef39

For 0.10.x, replace @call(.some_call_modifier, ...) with @call(.{ .modifier = .some_call_modifier }, ...) in these examples.

===Leave TRE to the compiler===
In this version, Zig compiler is free to (not) optimize recursive function, so behaviour may change from one optimization mode to another, like it was with 2-nd example from C section.
In this version, Zig compiler is free to (not) optimize recursive function, so behaviour may change from one optimization mode to another, like it was with 2-nd example from C section.
<syntaxhighlight lang="zig">const std = @import("std");
<syntaxhighlight lang="zig">const std = @import("std");
Line 3,410: Line 3,413:


===Force-disable TRE===
===Force-disable TRE===
'''Works with:''' 0.11.x, 0.12.0-dev.1381+61861ef39

To force-disable "simple tail recursion elimination" (STRE) for all optimize modes, we can use "never_tail" field of enum "std.builtin.CallModifier". It works not as hint, but as a hard requirement, so if it's impossible to fulfill, compile error is outputted.
To force-disable "simple tail recursion elimination" (STRE) for all optimize modes, we can use "never_tail" field of enum "std.builtin.CallModifier". It works not as hint, but as a hard requirement, so if it's impossible to fulfill, compile error is outputted.
<syntaxhighlight lang="zig">const std = @import("std");
<syntaxhighlight lang="zig">const std = @import("std");
Line 3,431: Line 3,432:


===Force-enable TRE===
===Force-enable TRE===
'''Works with:''' 0.11.x, 0.12.0-dev.1381+61861ef39

Similarly, we can force-enable mentioned optimization in all optimize modes by using enum field "always_tail". It's (again) a hard requirement and will emit compile error if this requirement is impossible to complete.
Similarly, we can force-enable mentioned optimization in all optimize modes by using enum field "always_tail". It's (again) a hard requirement and will emit compile error if this requirement is impossible to complete.
<syntaxhighlight lang="zig">const std = @import("std");
<syntaxhighlight lang="zig">const std = @import("std");