4 ms·
Nice article, learned a lot about Swift, love the named parameters and argument labels, makes calling functions with many parameters much more reliable and with
by garyclarke27 7y ago
Nice article, learned a lot about Swift, love the named parameters and argument labels, makes calling functions with many parameters much more reliable and with labels reads like English, which is great for immediate comprehension. Lack of Named parameters in Javascript, is for me, a critical glaring missing capability, that should be fixed urgently.
- enlyth 7y agoJust use an object with keys as the parameter names, this pattern is actually very common so I don't see the issue. doStuff({ foo: 3, bar: 7 })
- tomp 7y agoIndeed. What that pattern also solves, and AFAIK most static languages lack, is argument forwarding (though Python solves this even more elegantly than JS): def f(x, **kwargs): return g(x, foo=1, **kwargs)
- zeroxfe 7y agoIsn't the JS version just: function f(x, ...args) { return g(x, ...args); } Or did you mean forwarding object fields? function f(x, params) { return g(x, {foo: 1, ...params}); }
- tomp 7y agoAFAIK the first only works for positional (non-keyword) arguments. The second works, I guess? Not an expert in JS, especially not the "new parts", splats are definitely an improvement!
- garyclarke27 7y agoJS parameters are named to some extent, as in you can refer to them by name within the function body - BUT (a critical shortcoming in my view) you can’t assign values by name when calling functions, you can only assign values by position.
- jakear 7y agoSyntax special-cased to function parameters/arguments is more elegant than reusing the same object&spread syntax everywhere to achieve the same effect? I don't think so.
- saagarjha 7y agoC++ has argument forwarding among its many metaprogramming tricks.
- garyclarke27 7y agoYes an object is better than nothing, but is awkward syntax compared to proper named parameters and I don’t see it being used very often. In Postgres I find named paremeters far more convenient than positional paremters for calling functions, also conveys intention much better, also great for defaults (you can use default=null to get around ordering retstriction Postgres has).
- valuearb 7y agoYep, typeless Javascript can do anything except catch errors.