5 ms·
I am not aware of any language that supports overriding _some_ of the defaults from the function declaration, e.g. foo(1,,,'abc',,) Meaning that if you ne
by huhtenberg 9y ago
I am not aware of any language that supports overriding _some_ of the defaults from the function declaration, e.g.
foo(1,,,'abc',,)
Meaning that if you need to override a default for, say, 2nd argument, you will also need to explicitly provide all arguments that follow.
- lccarrasco 9y agoPython lets you override only those that you want using Named Parameters as explained in this answer: https://stackoverflow.com/questions/25374814/multiple-default-values-for-function-arguments-python https://stackoverflow.com/questions/25374814/multiple-defaul...
- conradfr 9y agoAs an alternative to named parameters I've always wanted a "default" keyword for function calling. function bar($word1, $word2='loves', $word3='Elixir') { return sprintf('%s %s %s', $word1, $word2, $word3); } $foo = bar('Hacker News', default, 'PHP'); But I guess functions with too many arguments are frowned upon and it would encourage it.
- wolco 9y agoIf you want to exclude the parameters after 'abc' you can. Better yet in php 7 using the splat operator ... foo(...$args) Allows you to send in any number of args.
- arethuza 9y agoI was thinking of languages (e.g. python and c#) where you can specify default values for named parameters.
- fiddlerwoaroof 9y agoIn some situations (destructuring, at least) JavaScript will use the default value when it encounters undefined.
- stiGGG 9y agoSwift can do this.