News for hackers. Stuff that matters.
Re:Named arguments are coming in PHP 8 by thaumaturgy on Monday July 27, 2020 @01:39AM (#23962492)
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 mud.

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.

But there is a common anti-pattern where you have some polymorphic function or interface, and you want to accrete your arguments somewhere and then bundle them up and call the function, so then you see things like [1]:

    call_user_func($function, ['length' => $n, 'foo' => $bar, ...]);
...and that is bad because it totally bypasses all of PHP's type-checking and leaves it up to the called function to sanity-check the input types instead of letting the compiler do it, and basically nobody does that anyway.

So named parameters would fix that at least, which would be nice. I'm just not looking forward to the impact this is going to have on code readability, because everyone (including this article) is going to go, "oh, now my parameter list is too long!", and break the function signature into one-line-per-parameter, which is vile.

[1]: I'm guilty of this too.

Re:Named arguments are coming in PHP 8 by Aeolun on Monday July 27, 2020 @01:42AM (#23962505)
> 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.

Re:Named arguments are coming in PHP 8 by masklinn on Monday July 27, 2020 @08:14AM (#23964329)
> Nobody will use this for functions that have just two parameters. It’s the ones that have 10 possibilities that are crazy.

It can be super useful even for functions with low arity e.g. `max(seq, key=func)`.

Re:Named arguments are coming in PHP 8 by arcosdev on Monday July 27, 2020 @02:07AM (#23962594)
Why not use a dictionary or parameter object when you get past 2 arguments? Or is that not a thing in PHP?
Re:Named arguments are coming in PHP 8 by raziel2p on Monday July 27, 2020 @03:35AM (#23962955)
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.

Re:Named arguments are coming in PHP 8 by wonderlg on Monday July 27, 2020 @09:05AM (#23964671)
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
    })
Re:Named arguments are coming in PHP 8 by duckerude on Monday July 27, 2020 @11:43AM (#23966169)
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.

Re:Named arguments are coming in PHP 8 by xellisx on Tuesday July 28, 2020 @09:53PM (#23982740)
We really need structs in PHP. I drew up a draft, but didn't get very far with it. https://github.com/ellisgl/PHP-RFC-Struct-Data-Type
Re:Named arguments are coming in PHP 8 by webignition on Monday July 27, 2020 @02:19AM (#23962642)
I'd think that such a solution doesn't solve but merely moves the problem.

A method with N arguments, where N > 2, with a call that is messy due to the number of arguments could instead take a single argument being a parameter object where the constructor for the parameter object takes the same N arguments.

A constructor with N arguments with a call that is messy due to the number of arguments ...

Re:Named arguments are coming in PHP 8 by SAI_Peregrinus on Monday July 27, 2020 @09:38AM (#23964942)
Aah, so then you use the Builder Pattern! Instead of one constructor taking N arguments, have a constructor that takes no arguments and N function calls to initialize the constructed values. It's cleaner because it's a named pattern! /s
Re:Named arguments are coming in PHP 8 by flemhans on Monday July 27, 2020 @02:12AM (#23962611)
That's what the above comment was referring to when saying people to things like:

  call_user_func_array($function, ['length' => $n, 'foo' => $bar, ...]);
Re:Named arguments are coming in PHP 8 by stareatgoats on Monday July 27, 2020 @02:48AM (#23962752)
> and basically nobody does that anyway.

That's it, I'm officially a nobody :-)

Re:Named arguments are coming in PHP 8 by sildur on Monday July 27, 2020 @02:59AM (#23962805)
> leaves it up to the called function to sanity-check the input types instead of letting the compiler do it, and basically nobody does that anyway.

You don’t check the input types?

Re:Named arguments are coming in PHP 8 by stareatgoats on Monday July 27, 2020 @04:46AM (#23963303)
You're reading the sentence incorrectly: "and basically nobody does that anyway", means the preceding type checking argument you refer to is moot. So I insist, I am a nobody (according to this definition).

Btw I don't always sanity check the parameters, mostly at the system edges only.

Re:Named arguments are coming in PHP 8 by z92 on Monday July 27, 2020 @05:23AM (#23963483)
Not in get-it-done, hackish scripts.
Re:Named arguments are coming in PHP 8 by Lammy on Monday July 27, 2020 @03:48AM (#23963016)
You can get the best of both approaches in PHP 5.6 or later using the argument unpacking "..." operator that works similarly to the */"splat" operator in Python or Ruby: https://www.php.net/manual/en/migration56.new-features.php
Re:Named arguments are coming in PHP 8 by gremlinsinc on Monday July 27, 2020 @09:47AM (#23965027)
it's not the best of both worlds though, splat should only be used on something where you're basically doing the same thing to a bunch of things.

like add(1,2,3,4,5) ->

  function add(...$nums){
    $count = 0;
      foreach($nums as $num) { $count += $num; }
  }
If you know what arguments you're getting, then the more sound way would be just use an associative array, but either way you really don't get great type checking.

This is a much better solution.

Re:Named arguments are coming in PHP 8 by gremlinsinc on Monday July 27, 2020 @09:42AM (#23964985)
I do this all the time, but it feels wrong... and you don't get type checking, this feels like it makes more sense and logical.
Re:Named arguments are coming in PHP 8 by beberlei on Monday July 27, 2020 @03:16AM (#23962877)
That is possible, but requires writing a class, which increases mental load
Re:Named arguments are coming in PHP 8 by gvjddbnvdrbv on Monday July 27, 2020 @03:36AM (#23962960)
I've tried it but it gets messy.
Re:Named arguments are coming in PHP 8 by dariusj18 on Monday July 27, 2020 @05:18PM (#23969501)
I'll use it for functions with two parameters, array functions and the order you pass in arguments is not standardized. I can start just using named functions to make it clear and easier to read/write.
Re:Named arguments are coming in PHP 8 by waheoo on Monday July 27, 2020 @04:53AM (#23963339)
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.

Re:Named arguments are coming in PHP 8 by raziel2p on Monday July 27, 2020 @06:44AM (#23963822)
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?

Re:Named arguments are coming in PHP 8 by chriswarbo on Monday July 27, 2020 @10:46AM (#23965553)
> 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 by raziel2p on Monday July 27, 2020 @11:44AM (#23966186)
So one of your arguments is that it'll be difficult to maintain the C implementation of call_user_func, call_user_func_array and func_get_args? But this has already been done by the PHP maintainers.

As for your argument that it breaks code that today relies on argument order... Even in current versions of PHP, nothing in the language stops me from changing function f($a) to function f($b, $a) between v1 and v2 of my library and your code is equally broken. Whether I as a function author decide to re-order my arguments is a decision informed but not dictated by the existence of named arguments.

I don't understand your rant about bandwagons/monoculture. If you ask me the reasons most popular langauges are moving in the same direction is because popular languages are stealing each others' best ideas, which is a good thing. All your "alternative" solutions read like horrible code to me compared to the now commonly accepted designs.

Re:Named arguments are coming in PHP 8 by chriswarbo on Monday July 27, 2020 @04:51PM (#23969237)
> So one of your arguments is that it'll be difficult to maintain the C implementation of call_user_func, call_user_func_array and func_get_args?

No, I wasn't talking about C; or about language implementations at all. I was arguing that the phrase "just allowing named parameters" gives the impression that this feature would only affect function calls and definitions, when that's not the case. The most obvious repercussions are for `call_user_func`, `func_get_args`, etc. whose API would need to change. That induces changes to code that calls those functions, and code that calls that code, and so on.

Although PHP is famously un(der)specified, I'm talking about things like "`call_user_func(f, args...)` is equivalent to `f(args...)`"; anything relying on that property will break in the presence of named parameters (since they may be given in a different order), unless patched to take it into account.

> Whether I as a function author decide to re-order my arguments is a decision informed but not dictated by the existence of named arguments.

Sure, but that's just a first-order effect. It's a breaking change for higher order code (where functions and arguments are data, and calling is an operation on that data). That's why I gave an example of higher-order code.

As a silly analogy: if PHP extended its default numeric types to be complex numbers, that wouldn't affect existing values like `$age = 18` or `$length = 50`, since they're "informed, but not dictated by the existence of complex numbers"; it also wouldn't affect calculations using these values like `plus($age, $length)`. Yet it would have profound effects on numeric functions, which may have to be patched to take complex values into account: some functions, like `array_sum`, might work fine as-is; whilst others, like `array_slice`, might make no sense given the new situation (how might we take `3+5i` elements from an array?).

> All your "alternative" solutions read like horrible code to me compared to the now commonly accepted designs.

This sentence is a good example of what my "rant" is about. Why should improvements depend on what's "commonly accepted" (surely that's antithetical)? Could you elaborate any further on what makes those examples "read like horrible code"?

As a more detailed example we can look at `switch`: it's clearly a "now commonly accepted design", due to its prevalence in many "popular languages" like PHP/Javascript/etc. An alternative approach which solves the same problem is `match`, as used by ML. However, ML is not a popular language, and ML also introduced currying which you say "read[s] like horrible code". Does that make `switch` better than `match`?

This is an interesting example since a recent Python Enhancement Proposal is trying to add `match` to the language. This proposal seems credible, since the author list includes Guido van Rossum (the creator of Python): https://www.python.org/dev/peps/pep-0622

Perhaps this is an outlier, since Python doesn't already have an equivalent like `switch`?

I wonder if the existence of that PEP changes your opinion of `switch` versus `match`? Maybe not; after all, Python doesn't have `switch` so maybe `match` is better than nothing. What if I showed you an equivalent proposal for adding it to Javascript (using the keyword `case`, like the non-popular language Haskell)? https://github.com/tc39/proposal-pattern-matching

At what point does a feature go from "horrible code" to "stealing each others' best ideas"?

What makes this even more interesting is that the very first sentence of PEP622 makes it clear that this is a case of stealing a good idea:

> This PEP proposes to add a pattern matching statement to Python, inspired by similar syntax found in Scala and many other languages.

To add a further wrinkle, PEP3103 was rejected for proposing essentially the same idea back in 2006: https://www.python.org/dev/peps/pep-3103/

Perhaps it was too soon to adopt ideas from Scala, since it had only been out for a couple of years by that point. However, `match` wasn't invented by Scala; it's been around in multiple languages, some more popular than others, dating back to 1973!

As further evidence of this, there was another rejected PEP for the same basic idea, from way back in 2001: https://www.python.org/dev/peps/pep-0275

I wonder whether your line of reasoning would come out in in favour of PEP622 today, whether it would have come out in favour of PEP275 twenty years ago, and whether it will argue in favour of `match` twenty years from now?

Re:Named arguments are coming in PHP 8 by Lammy on Monday July 27, 2020 @03:50AM (#23963025)
One-line-per-argument function signatures are nice for making clean diffs :)
Re:Named arguments are coming in PHP 8 by beberlei on Monday July 27, 2020 @03:16AM (#23962873)
Named params helps the beginners, maybe even more for use of internal APIs

- no needle/haystack order problems anymore - no need going to docs to understand what a boolean or „magic value“ stands for

Re:Named arguments are coming in PHP 8 by gregmac on Monday July 27, 2020 @10:19AM (#23965306)
Not just beginners. Seeing the following in a pull request is bad for everybody:

    someFunction(username, true, false, 10)
Named parameters remove ambiguity and make the code easier to read and review, and even avoid bugs because if you're passing the wrong values it's immediately obvious.

One of the hidden issues with the above code comes in the future: if anyone ever changes the order of the 2nd and 3rd arguments in the function definition, and forgets to update this spot, the code will still "work" (no compile error) but instead will have the wrong runtime behaviour. In a pull request, you can't see all the affected spots that didn't get updated, so it is easy to slip through.

Re:Named arguments are coming in PHP 8 by coldtea on Monday July 27, 2020 @10:23AM (#23965330)
>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 by stinos on Monday July 27, 2020 @02:31AM (#23962688)
who want ... and many deeply-nested complex class hierarchies

Are there really people aiming for that, as a goal? Or is it rather just the result of large-scale software? And/or if it's really complex, then perhaps that's just the result of suboptimal design, not the intent?

Re:Named arguments are coming in PHP 8 by flukus on Monday July 27, 2020 @03:00AM (#23962814)
Yes, sort of. There are many people that think of OOP as the goal and not the tool, that a more OOP solutions is better. Some of them equate complex class hierarchies with a job well done.

I'm being a bit hyperbolic, but I've worked that think a if/switch statements are a code smell that should be converted to complex class hierarchies.

Re:Named arguments are coming in PHP 8 by cyphax on Monday July 27, 2020 @07:28AM (#23964058)
I can sympathize with that, but at the same time: the idea that the language and its OOP implementation is a tool rather than the goal, and the idea of improving said tool are not mutually exclusive. :)

In my experiences with C#, things like named params have been nice, even if it's often just syntactic sugar; these things can be quality of life improvements for the programmer without changing anything about the goal.

Re:Named arguments are coming in PHP 8 by iso8859-1 on Monday July 27, 2020 @02:18AM (#23962640)
You can have typed dictionaries too, even in a language similar to PHP: https://mypy.readthedocs.io/en/stable/more_types.html#typedd...

But anyway, I'd prefer anonymous records like in F#. There's no reason to have static and dynamic mappings have the same syntax, especially if they're not the same to the type checker, even a compiler could statically dispatch the static variant. So I agree with you that dicts are overused.

Re:Named arguments are coming in PHP 8 by pansa2 on Monday July 27, 2020 @03:59AM (#23963071)
> There's a tension in PHP-land between [...] hobbyist-hacker [vs] enterprise-scale

IMO the same tension exists in Python. The hobbyist-hackers are using the language to write small-ish scripts, and the enterprise-scale developers are working on million-line codebases.

Unfortunately for the hobbyist-hackers, at some point (perhaps when the BDFL started working at Dropbox), the focus of the language shifted from the first group to the second. That's why, for example, Python now has a complex type system - that many developers never use.

Re:Named arguments are coming in PHP 8 by iso1631 on Monday July 27, 2020 @08:19AM (#23964368)
> IMO the same tension exists in Python. The hobbyist-hackers are using the language to write small-ish scripts, and the enterprise-scale developers are working on million-line codebases.

It's an endless cycle in all aspects of IT, the conflict between small tech and enterprise tech

Large tools from bureaucratic enterprises are useless (that's a given)

Shadow IT built around hacker tools gets the business goals through

Enterprise IT see that these tools are popular and actually meet business goals, but they didn't invent them

Enterprise IT attempts to justify itself and find something approximating the usability of the good tools from some "safe" vendor like Oracle, Microsoft, etc. After all nobody got fired for buying IBM.

Enterprise IT spends a fortune, everyone hates it, but old tools are defunded so can't be used.

Shadow IT build new ecosystem around different hacker tools to get the business goals through

And the cycle continues.

Re:Named arguments are coming in PHP 8 by majewsky on Tuesday July 28, 2020 @07:25AM (#23974567)
> Large tools from bureaucratic enterprises are useless (that's a given)

There are plenty useful tools that are large and built by bureaucratic enterprises. Stuff like ERP systems just has so many features to cover and tax laws to implement that it most likely can genuinely not be built by a small team.

(Disclosure: I work for such an enterprise, though I'm not involved in the development of software sold to customers.)

Re:Named arguments are coming in PHP 8 by oaiey on Monday July 27, 2020 @04:22AM (#23963182)
I would argue that has a strong correlation with the fact that universities are now teaching Python instead of Java. Once that alumni hits the job market, hobbyist-hackers are no longer the majority of users.
Re:Named arguments are coming in PHP 8 by rglullis on Monday July 27, 2020 @07:51AM (#23964183)
You'd have a point if there were any changes that are being introduced and putting developers on a straight-jacket. But I don't see this tension at all.

What is recent on Python3 that makes it hostile to hobbyists? The new features are optional, it's not like Python is forcing people to adopt mypy.

Re:Named arguments are coming in PHP 8 by pansa2 on Monday July 27, 2020 @09:17AM (#23964758)
> What is recent on Python3 that makes it hostile to hobbyists?

Python has become a very complex language [0], and the complexity continues to increase. IMO it's too much for people who only write code for, say, a few hours a week.

> The new features are optional

I hope Python isn't becoming a "choose your own subset" language like C++.

[0] https://twitter.com/raymondh/status/1280946969116995584

Re:Named arguments are coming in PHP 8 by rglullis on Monday July 27, 2020 @10:16AM (#23965287)
The tweet shows a clear list of features (aside from the walrus operator) that are "complex" because they deal with inherit complexity. People who write code only for a few hours a week don't need to deal with asyncio or parallel programming, typing, cython, etc, etc.

The point about different frameworks and ecossytem is a strawman. Yes, it's large. No, it doesn't mean that is complex. No, you don't need to know all of it.

> I hope Python isn't becoming a "choose your own subset" language like C++

You are implying that are what is being added to the language have overlapping functionality. You really need a better argument than a FUD-y tweet to justify your concerns.

Re:Named arguments are coming in PHP 8 by Qem on Tuesday July 28, 2020 @02:38PM (#23979200)
What current languages do you think are friendly to hobbysts, with low complexity overhead, like Python before the enterprise-scale refocusing?
Re:Named arguments are coming in PHP 8 by masklinn on Monday July 27, 2020 @08:17AM (#23964357)
> Python now has a complex type system - that many developers never use.

Python doesn't have a complex type system. How could it when, to this day, it doesn't bundle a type-checker?

Python had a way to annotate functions (PEP 3107), which the community largely used for type-hinting, so Python introduced better support for type hints (PEP 484 and various extensions since).

Re:Named arguments are coming in PHP 8 by pansa2 on Monday July 27, 2020 @09:28AM (#23964861)
OK, I'll rephrase that as "Python now has a complex typing ecosystem". That seems to be the preferred phrase - for example, it's used in PEP 586 [0].

[0] https://www.python.org/dev/peps/pep-0586/

Re:Named arguments are coming in PHP 8 by detaro on Monday July 27, 2020 @08:21AM (#23964377)
That many developers never use type annotations (nothing interesting has happened to the type system in a long time) kind of suggests that it's not a problem. The feature is there if you want it, if you don't you ignore it.
Re:Named arguments are coming in PHP 8 by pansa2 on Monday July 27, 2020 @09:42AM (#23964993)
Type annotations aren't entirely optional, though. For example, they're required if you want to use data classes.

Even if they were optional, that would still be a problem. As above, too many optional features are turning Python into a "choose your own subset" language like C++.

Re:Named arguments are coming in PHP 8 by tannhaeuser on Monday July 27, 2020 @03:36AM (#23962966)
> tension in PHP-land between PHP's roots as a low-ish level, get-it-done, hackish language [...] and the better-organized and quite vocal developers who want it to be more Java-like

Agree, and I'd like to add the original purpose of PHP, and what made it popular in the first place was that it can be used as embedded PHP within SGML-ish processing instruction ie <!php ...>. Only that PHP made such a lousy hack job of it being not HTML-aware and not doing context-dependent escaping, that it would become the primary script injection/XSS attack vector on the web. PHP's forte IMHO is its community and the large installed base of apps (there doesn't exist anything like Magento with its first-party plugins by DHL, UPS, payment providers, etc on Node.js for example). Why PHP wants to become more like Java or JavaScript is a mystery for me, when there's a hell of a cleanup job to be done beforehand, such as fixing <?php ...>.