On 20/01/2015 20:09, Marc Bennewitz wrote:

Am 19.01.2015 um 19:48 schrieb Adam Harvey:
On 17 January 2015 at 18:04, Andrea Faulds <a...@ajf.me> wrote:
For consistency with list(), we could also just put nothing:


     foo($bar, , $baz);

Which is like:

     list($bar, , $baz) = $arr;

Thoughts?
That was Stas's original, original proposal way back when. I argued
then for having "default" as a placeholder, and still would today — in
the case where a function with, say, ten optional parameters[0] is
being called and eight of them should be the default, I think it's a
bit rough for somebody inheriting that code to have to count commas.
Having a token increases readability, IMO, and costs us nothing except
a few keystrokes, which isn't going to break the camel's back in a
language that requires "function" each time you declare a function.

Adam

[0] Yes, that's probably poor API design. You and I both know someone
will do it, though. :)

Pros & Cons

_Simply skipping the argument out:_
pro:
    - same syntax as skipping entries used by `list`
con:
    - hard to read and to differ from mistakes
    - not an option for inheritance

_Using the keyword `default`:_
pro:
    - better to read and to differ from mistakes
    - usable on inheritance
con:
    - different syntax as `list`
- not possible as alternative syntax for `list` as it has no "default" naming

_Using a special character:_
pro:
    - better to read and to differ from mistakes
    - usable on inheritance
    - possibly an alternative syntax for `list` (for consistency)
con:
    - different syntax as `list`
    - Not a good character found for it, yet

_Named Parameters:_
pro:
    - readability  (If caller is not required to call as assoc array)
    - not an option for inheritance
con:
    - paramter skipping is a side product with possible edge cases
    - Adds variable names to be part of the API
    - implementation complexity

In my opinion plain skipping parameters is a nice addition to PHP even if named arguments will be introduced.

A special character would indeed the best option but as long as there is no good character it's not an option.

Nice summary. My only difference of opinion would be that a special character, even if a good candidate were found, should be listed as "pros: better readability than simply skipping; cons: worse readability than a keyword". Punctuation is never going to be as intuitive as a well-chosen keyword for someone new to the feature.

It certainly has advantages in applicability to multiple situations, though. The mention of for..in elsewhere made me wonder about a syntax for discarding the values of an array when looping over its keys; if _ were available as a default/skip token, you could write:

foreach ( $foo as $key => _ ) { ... }

That doesn't make sense with the "default" keyword, although a different keyword might fit multiple use cases, e.g. "skip":

func_with_many_args(42, skip, skip, false);
list($foo, skip, $bar) = $some_array;
foreach ( $foo as $key => skip ) { ... }

That actually reads quite well to me, but obviously requires reserving a new keyword, and breaking compatibility if any code has a constant named "skip".

Regards,

--
Rowan Collins
[IMSoP]


--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to