On Sunday, 8 May 2022 at 03:58:06 UTC, Tejas wrote:
[snip]

If there is only one possible value for the overload, is there an issue with using default arguments?
[snip]

Default arguments are intended to be resolved at runtime. That is, if you compile a function with two parameters and one of them has a default, then the compiler will compile one function that has two parameters as inputs.

However, since `foo` above is a relatively simple function, if you compile with -O, then it gets inlined. It doesn't matter so much that whether `a` is an enum or a literal since the compiler knows what it is at compile time and will inline it to remove the call to `foo` anyway.

I am interested in more complex cases where the compiler isn't able to inline the function and where the behavior of the second parameter might be more significant. The default parameter would then be doing the calculation at runtime when ideally may be known at compile-time and the compiler could generate a separate function that is simpler taking only one parameter.

Reply via email to