Live data from Hacker News

Named arguments are coming in PHP 8

stitcher.io

121–130 of 140 posts

Re: Named arguments are coming in PHP 8

#121

I'm ambivalent. There's a tension in PHP-land between PHP's roots as a low-ish level, get-it-done, hackish language, with its big standard library and simple scalar types, and the better-organized and quite vocal developers who want it to be more Java-like, with great big frameworks and many deeply-nested complex class hierarchies. Instead of unwieldy hobbyist-hacker balls of mud, you build enterprise-scale balls of…

>Named arguments don't do a lot for the first group. It looks like the big point in favor is not having to look up a reference for which-arguments-go-where in functions anymore, but a good IDE already does that for you.

I, for one, never particularly liked this last argument.

Re: Named arguments are coming in PHP 8

#122
post #81

> named arguments allow you to pass input data into a function, based on their argument name instead of the argument order Granted, I'm not a PHP developer, but I can't understand why a change like this would be controversial. It sounds like it's optional, and would help greatly to reduce mistakes when passing arguments to functions? Having multiple lines of things being passed to a function may look odd to some, but…

[deleted]

Re: Named arguments are coming in PHP 8

#123
post #81

> named arguments allow you to pass input data into a function, based on their argument name instead of the argument order Granted, I'm not a PHP developer, but I can't understand why a change like this would be controversial. It sounds like it's optional, and would help greatly to reduce mistakes when passing arguments to functions? Having multiple lines of things being passed to a function may look odd to some, but…

As a python dev my mind is kinda blown that php didn't have keyword arguments yet. Especially with the current trend of using kwargs exclusively for better code quality and readability.

Re: Named arguments are coming in PHP 8

#124
I'm so happy to see named parameters become more common.

They're absolutely critical to writing code that is easy to read. The name of a function tells you what it does, arguments equally need names to tell you what they do.

Even if I have a great IDE, I don't want to hover/cursor over the line to see what each argument does, each random "0" or "null" or "1" or whatever. There are also plenty of times I'm viewing code that isn't in an IDE, like a diff.

Obviously they're not always needed, like in functions that take only one or two required obvious parameters.

But otherwise, they can make the intent of a line of code obvious at a glance, and are a huge step toward making code self-documenting.

I just can't believe this is a change that's only picking up now, rather than 20 years ago.

Re: Named arguments are coming in PHP 8

#125
post #81

> named arguments allow you to pass input data into a function, based on their argument name instead of the argument order Granted, I'm not a PHP developer, but I can't understand why a change like this would be controversial. It sounds like it's optional, and would help greatly to reduce mistakes when passing arguments to functions? Having multiple lines of things being passed to a function may look odd to some, but…

Granted, I'm not a PHP developer, but I can't understand why a change like this would be controversial. It sounds like it's optional, It's only optional when you're writing code. Even if you don't use features in your own projects you're undoubtedly going to be reading a lot of other's code, which may or may not use these features, and from that perspective added language complexity is a burden.

Any framework/library code which abstracts over invoking a callable must also be extended to support associative arrays, and to avoid assuming that parameters will be given in the order they're defined. For example, things which play around with call_user_func and func_get_args (e.g. delayed evaluation, partial evaluation, currying, mapping over datastructures, composing/pipelining, etc.)

Re: Named arguments are coming in PHP 8

#126
post #51

Earlier quoted context omitted.

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?

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

That "just" is doing a lot of work. I've not used PHP for a few years, but off the top of my head I can forsee the following problems:

- `call_user_func` would need to change to support this, e.g. accepting and passing-along named parameters.

- `call_user_func_array` would need to change, e.g. passing along associative-array arguments as named parameters.

- `func_get_args` would need to change, e.g. returning an associative array

Other parts of the standard library, like ReflectionParameter, would also need corresponding changes.

On their own I don't think those are necessarily bad. However, this would break a lot of code which relies on those functions and their invariants. For example, currying and partial application would become more complicated (e.g. http://chriswarbo.net/blog/2014-02-21-partial_application___... ).

I'm not necessarily against this change, but I would make the following points:

Extending APIs is a breaking change, since it breaks invariants/contracts that previously held (in this case by function definitions and call sites). This breaks backwards-compatibility, and that should be acknowledged and stated explicitly, rather than ignored. Here's a quick example of code which breaks, since it relies on the now-violated invariant that the parameter order of a function call matches that of the definition:

    function call_with_strings() {
      $args    = func_get_args();
      $f       = array_shift($args);
      $strings = [];
      foreach ($args as $arg) {
        $strings[] = strval($arg);
      }
      return call_user_func($f, $strings);
    }
Secondly: there's a definite bandwagon effect between scripting languages like PHP/Python/JS/etc., for better (easier to transfer between) or worse (uncompelling monocultures lacking innovation/USP). Just because a feature exists in one, doesn't necessarily make it a good idea for the others (or the first, for that matter!); conversely, there are lots of great features out there which scripting languages don't seem to have picked up, and which are hardly ever brought up in these discussions. For example:

- Default arguments are generally supported, e.g. `parseInt(i, base=10)`. Alternative approaches to the same problem, like currying, are mostly absent, e.g. `parseIntFor(base, i); parseInt = parseIntFor(10);`

- Generators are generally supported, e.g. `for x in foo: yield x`. Alternative approaches to the same problem, like delimited continuations, are mostly absent, e.g. `reset; for x in foo: shift(x);`. This is especially interesting, since it's just an extension of exception handling, which is another generally-supported feature!

- Named parameters are generally supported, e.g. `foo(a=1, c=5)`. Alternative approaches to the same problem, like row-polymorphism, are mostly absent, e.g. `foo(['a' => 1, 'c' => 5])`.

Re: Named arguments are coming in PHP 8

#127

I'm so happy to see named parameters become more common. They're absolutely critical to writing code that is easy to read . The name of a function tells you what it does, arguments equally need names to tell you what they do. Even if I have a great IDE, I don't want to hover/cursor over the line to see what each argument does, each random "0" or "null" or "1" or whatever. There are also plenty of times I'm viewing co…

On the other hands, many JetBrains IDEs will insert the names of parameters in a function call inline to help identify what passed variable belongs to which parameter. It’s not inserting it in the actual source code, but exists purely in the UI (https://www.jetbrains.com/help/clion/parameter-hints.html). It’s useful if $LANG doesn’t have named parameters

Re: Named arguments are coming in PHP 8

#128

Earlier quoted context omitted.

I understand that this is standard practice by now, but this is one of those cases where I can't help but feel the language design team strayed too far from the original spirit of python. Now there's yet another possible interpretation of an asterisk: multiplication; splatting; exponentiation; declaring variadic arguments; and now declaring that all subsequently declared arguments are to be keyword-only. And I unders…

> This runs counter to the Zen: "Readability counts", "Explicit is better than implicit" Huh? It's both making parameters more explicit and functions using it more readable and self-documenting. In fact, the old way, before bare / and * in args list, where functions essentially had mandatory optional-keyword arguments made every function create an API in violation of “There should be one-- and preferably only one --o…

I’m not interested in comparing it to the old way, that’s saying “this unintuitive way of doing things is a better way of doing them than before when they were impossible”. Achieving that is a too low a bar to be worth discussing.

It is very much not readable, as evidenced by the other python-experienced commenters on this thread having no clue what it means (“readability” doesn’t mean “easy to understand for people who already know what it means”, as achieving that is a very low bar. What should be targeted is “obvious enough to have a clear meaning even to those who haven’t studied the exact section of the spec”, which is possible in the JS approach as they’re reusing the same syntactic constructs throughout the language, but is impossible in the Python approach because they introduce new syntax to work around every little limitation of their existing syntax)

Re: Named arguments are coming in PHP 8

#129
post #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…

JS records are superior.

I can create an object, then dynamically add various parameters before sending them rather than bogging down the function call. Destructuring in particular ensures this is pleasant for both the function creator and consumer.

You see a similar debate between StandardML and Ocaml though on a more fundamental level. SML uses structurally-typed records, so you can instantiate an inline record without needing to add any types and the function will destructure it for use. Ocaml went with nominal typing making this painful, so they added a complex optional argument syntax instead. Typescript is also structurally typed and I'd guess this is the reason.

Re: Named arguments are coming in PHP 8

#130

Earlier quoted context omitted.

With a dictionary (array in PHP) you lose strict typing and it's possible for the person constructing it to make a typo and it won't cause an error, the called function will just think an optional argument was left out. Writing and maintaining parameter classes is just tedious.

Typescript can do it, so why not add the same feature to PHP as well? As a JS/TS developer, all I see is avoiding two characters: {}, everything else is possible in JavaScript and well-typed in TypeScript. PHP 8 save( value: 3 ) JS 1 save({ value: 3 })

PHP's strict typing is only enforced at runtime, and because of that, its type system is very simple, unlike Typescript's. You can enforce that a variable is an array, but not that it's an array of ints, let alone that it's an array with certain types under certain keys.

Although there is external tooling that uses static analysis for such more complicated types.

Post reply on HN