Live data from Hacker News

Named arguments are coming in PHP 8

stitcher.io

61–70 of 140 posts

Re: Named arguments are coming in PHP 8

#62

Not sure if the standard library has evolved since my PHP days, but this would at least solve the "key / haystack" issue. In which lots of the standard library had different orders in which parameters are passed. One can finally pass them named and no longer have to care about the order. This may also have been solved by better tooling since then, though.

a proper IDE solves that, but i always laugh when getting back to php why they made array_map and array_filter switch parameter order.

Re: Named arguments are coming in PHP 8

#63

There are a lot of arguments surrounding this subject. Just came in here to chime about how I miss named arguments in c++. Sure, I can emulate them [0] but at the cost of ide support, runtime and compile time. https://github.com/nurettin/pwned/blob/770144a15efe7ab96bf4d...

They are effectively in C++20 without real penalty (designated initializers).

Re: Named arguments are coming in PHP 8

#64
So here's a good test for the value of [language feature X]:

Do people naturally try and reinvent that feature in its absence?

So for named parameters you have:

1. Javascript would often be written with methods with a single "options" parameter, where "options" is an anonymous object and basically a map;

2. Day-to-day I write Hack. Many functions are similarly written taking a shape as an argument. Shapes are structs, basically, but the typechecker is smart enough to treat them as a well-defined by anonymous type.

3. I've also seen C code that does much the same with things structs.

Empirically there seems to generally be a demand for this kind of feature and it makes sense: long parameter lists are tedious and error-prone (have you ever seen a function that has 3 boolean parameters in a row?) where people try and be helpful by providing "optional" parameters (which are really just parameters with default values).

So sure, I'm all for named parameters.

Re: Named arguments are coming in PHP 8

#65
post #63

There are a lot of arguments surrounding this subject. Just came in here to chime about how I miss named arguments in c++. Sure, I can emulate them [0] but at the cost of ide support, runtime and compile time. https://github.com/nurettin/pwned/blob/770144a15efe7ab96bf4d...

They are effectively in C++20 without real penalty (designated initializers).

Yes, designated initializers help initialize a dto, much like what C# did back in 3.0 times. I wish they did something similar to function parameters so we didn't even have to create a data transfer object.

Re: Named arguments are coming in PHP 8

#66

Not sure if the standard library has evolved since my PHP days, but this would at least solve the "key / haystack" issue. In which lots of the standard library had different orders in which parameters are passed. One can finally pass them named and no longer have to care about the order. This may also have been solved by better tooling since then, though.

a proper IDE solves that, but i always laugh when getting back to php why they made array_map and array_filter switch parameter order.

> why they made array_map and array_filter switch parameter order

It kind of makes sense if you look at the signatures (array_map takes a callable and a variable number of arrays, while array_filter takes a singe array and an optional calable), but yeah, it's horrible to use.

Re: Named arguments are coming in PHP 8

#67

It's beginning to look a lot like -- typescript

generally yes, but specifically here typescript does not have named parameters (yeah you can emulate with a hack through object literals which is sad)

What's sad about it? Having interfaces defining the arguments of functions with complex parameters in a type safe manner and with support for auto-completion is one of the greatest thing to ever happen to JavaScript in my opinion. And you get named function arguments as a by-product of something way more powerful than just that.

Re: Named arguments are coming in PHP 8

#68

I really love this new feature however my only concern is that renaming some parameters will trigger a lot of changes in other places as a side effect, thus polluting diffs.

I get what you mean, but I remember at one point going through some kind of 'oh no, it's a large diff' or even 'do not do this because the diff will increase' phase. And I realized that's just not ok. There's already enough things to keep in mind when programming, adding an extra one just because some external tool which actually doesn't have a lot to do with how the code works, i.e. holding back changes because of diff, just isn't worth it.

Now, I admit cluttered diffs can be a problem, but there are workarounds: more fine-grained commits (especially separate commits for renames only) and better diff display. As an example of the latter: another commnter mentions one argument per line produces better diffs. It does for your standard display with +++/---, but tools like Sublime Merge have this covered and siplay the change in such a way you can immediately see what argument changed it's name, inline in the code. If it's was just a typo fix it will usually only highlight the changed character(s) not the complete name. Even going from all arguments on one line to one agrgument per line is covered, since it'll just display it as added whitespace and won't highlight the argument themselves indiacting they did not change.

Re: Named arguments are coming in PHP 8

#70
post #51
post #8

Earlier quoted context omitted.

> break the function signature into one-line-per-parameter, which is vile Speak for yourself. If each line indicates what it is the parameter for I couldn’t care less. Nobody will use this for functions that have just two parameters. It’s the ones that have 10 possibilities that are crazy.

Other than plain object constructs these kinds of functions are unheard of and better handled with a parameter object or array. I can't help but feel if they just set up a clean way to name construct Params for classes this wouldn't need to happen.

Why are they better handled with a parameter object or array? https://news.ycombinator.com/item?id=23962955

What is the functional difference between a named construct "Params" and just allowing named parameters?

Post reply on HN