Hacker News new | past | comments | ask | show | jobs | submit login

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.



Just 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 })


Yep, typeless Javascript can do anything except catch errors.


Indeed. 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)


Syntax 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.


Isn'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});
    }


JS 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.


AFAIK 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!


C++ has argument forwarding among its many metaprogramming tricks.


Yes 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).




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: