7 ms·
I believe that if most foo's users should just call it with [], the Pythonic way is to make the argument optional.
by owl57 2y ago
I believe that if most foo's users should just call it with [], the Pythonic way is to make the argument optional.
- samatman 2y agoWell yes, but if it's someone else's library, realistically you're not going to change it. Zig is a static language without variadic parameters, so you can't make it optional in that sense. You could make the options a `?T` and pass `null` instead, but it isn't idiomatic, because passing `.{}` to a parameter expecting a `T` will fill in all the default values for you.
- xigoi 2y agoThis doesn’t answer the question why Zig doesn’t have default argument values.
- throwawaymaths 2y agoDefault argument variables create variadic functions. Arity N when you supply a value Arity N-1 when you use the default
- xigoi 2y agoOkay, but why could a static language not have variadic functions?
- throwawaymaths 2y agoThat's their design choice. I can think of a few reasons - makes function calling simpler - faster compilation - less symbol mangling - makes grepping for function implementation easier If for some reason you think you absolutely can't live without variadic functions, maybe don't use zig.
- Phil_Latio 2y agoHow does this create variadic functions? The arity is the same, since the function signature defines the exact amount of arguments. The compiler just passes the omitted ones for you.