3 ms·
This goes away in a language that allows arguments to name the parameters: fetch(accountId, history = true, details = false); However, I would be opposed if
by kazinator 2y ago
This goes away in a language that allows arguments to name the parameters:
fetch(accountId, history = true, details = false);
However, I would be opposed if this mechanism is permitted to perturb the order of fixed arguments. If history is the third argument, rather than the second, it should error.
I.e. "history = true" means "we are passing true as the second parameter, which we believe to be called 'history', on penalty of error".
Fixed parameters having their names mentioned and checked should not be confused with the language feature of keyword parameters, which passes an unordered dictionary-like structure.
- CoastalCoder 2y agoIs this an example from some well-known compiled language? I work mostly in C++, and this is a feature I've wished for for a long time.
- ijlx 2y agoSwift and C# appear to have this feature, though they use the syntax fetch(accountId, history: true, details: false);
- yen223 2y agoKotlin does this https://kotlinlang.org/docs/functions.html#named-arguments https://kotlinlang.org/docs/functions.html#named-arguments
- kazinator 2y agoNope! > "When you use named arguments in a function call, you can freely change the order that they are listed in." That violates a key requirement in my specification, which means that I regard it as broken. Positional parameters should take arguments only in their designated argument position. Only non-positional parameters (keyword parameters) should be allowed to be specified in any order.
- JamesonNetworks 2y agoIs there an example you have in mind as to why the params, if passed via keyword, would need to stay in their position? It sounds like external calls made to the internal api without the headers being present? Trying to understand why the position/order would matter to you once arguments are named?
- einburger 2y agoIf the code base supports designated initializers I'll use them as a way of getting named parameters like so: struct SomeArgs { bool normalize{false}; int stride{2}; char ch{'x'}; ... some other stuff }; void someFn(SomeArgs args); someFn({ .stride = 1, .ch = 'y' }); someFn({ .normalize = true }); not as simple as python but it's close-ish and more readable than: someFn(false, 2, 'c');
- gherkinnn 2y agoC# is well known I guess https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/named-and-optional-arguments#named-arguments https://learn.microsoft.com/en-us/dotnet/csharp/programming-...
- js2 2y agoObjective-C. Swift.
- adamrezich 2y agoJai has this.