Hi!
I've finally took some time to revive the skipping parameter RFC and patch. For those who doesn't remember what it is please see: https://wiki.php.net/rfc/skipparams
TLDR version:
The idea is to allow skipping parameters in function with optional arguments so that this:
function create_query($where, $order_by, $join_type='INNER', $execute = false, $report_errors = true)
can be called like this:
create_query("deleted=0", "name", default, default, /report_errors/ true);
Instead of trying to remember what the defaults are. The patch is here:
https://github.com/php/php-src/pull/426
Any comments or feedback on the RFCs and the code are welcome, especially pointing out the cases where it may not work (which means we need more phpt's there :)
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
Quoted text · 7 lines
Hi!
I've finally took some time to revive the skipping parameter RFC and patch. For those who doesn't remember what it is please see: https://wiki.php.net/rfc/skipparams
TLDR version:The idea is to allow skipping parameters in function with optional arguments so that this:
function create_query($where, $order_by, $join_type='INNER', $execute = false, $report_errors = true)can be called like this:
create_query("deleted=0", "name", default, default, /report_errors/ true);
Hey:
I am not sure whether this is a little overhead.
but if we do want this. to be honest, I even prefer leave it empty to "default".
like create_query("delete=0", "name", , , true);
thanks
Quoted text · 7 lines
Instead of trying to remember what the defaults are. The patch is here:
https://github.com/php/php-src/pull/426
Any comments or feedback on the RFCs and the code are welcome, especially pointing out the cases where it may not work (which means we need more phpt's there :)
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227--
--
Laruence Xinchen Hui
http://www.laruence.com/
Hi!
I am not sure whether this is a little overhead.
but if we do want this. to be honest, I even prefer leave it empty to "default".
like create_query("delete=0", "name", , , true);
That's how it was initially, and I was convinced default is better. So I don't think I'm going back to empty. default indeed seems to be more readable (try to visually distinguish between ,,,, and ,,,, - if you have a lot of code, it won't be that easy). It is more verbose, but PHP never attempted to be "save the bytes" language.
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
Quoted text · 9 lines
Hi!
I am not sure whether this is a little overhead.
but if we do want this. to be honest, I even prefer leave it empty to "default".
like create_query("delete=0", "name", , , true);
That's how it was initially, and I was convinced default is better. So I don't think I'm going back to empty. default indeed seems to be more readable (try to visually distinguish between ,,,, and ,,,, - if you have a lot of code, it won't be that easy). It is more verbose, but PHP never attempted to be "save the bytes" language.
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227--
Hi,
For me as a user it would make much more sense to have named parameters then typing default. Default also feels a bit strange to type. What if I miss one default in the line? Or in the other proposal here, what if I miss one comma?
Named parameters would also solve a problem from another RFC that's being proposed right now if I understand correctly so why not go for that?
Kind regards
Florin Patan
https://github.com/dlsniper
http://www.linkedin.com/in/florinpatan
On Mon, Sep 2, 2013 at 9:41 AM, Stas Malyshev [email protected]wrote:
Quoted text · 5 lines
Hi!
I am not sure whether this is a little overhead.
but if we do want this. to be honest, I even prefer leave it empty to "default".
like create_query("delete=0", "name", , , true);
That's how it was initially, and I was convinced default is better. So I don't think I'm going back to empty. default indeed seems to be more readable (try to visually distinguish between ,,,, and ,,,, - if you have a lot of code, it won't be that easy). It is more verbose, but PHP never attempted to be "save the bytes" language.
another small advantage would be that IDEs could show you the default value for the argument when hovering the default keyword.
--
Ferenc Kovács
@Tyr43l - http://tyrael.hu
Ferenc Kovacs wrote:
another small advantage would be that IDEs could show you the default value for the argument when hovering the default keyword. But a good IDE is already showing the full function and notes ... and will autocomplete so that this fix is not actually required :)
In many cases, the 'additions' being loaded into the core would be much better handled in an IDE and already were in am ore flexible way ...
--
Lester Caine - G8HFL
Contact - http://lsces.co.uk/wiki/?page=contact L.S.Caine Electronic Services - http://lsces.co.uk EnquirySolve - http://enquirysolve.com/
Model Engineers Digital Workshop - http://medw.co.uk Rainbow Digital Media - http://rainbowdigitalmedia.co.uk
Am Montag, 2. September 2013 um 11:02 schrieb Lester Caine:
Quoted text · 4 lines
Ferenc Kovacs wrote:
another small advantage would be that IDEs could show you the default value for the argument when hovering the default keyword.
But a good IDE is already showing the full function and notes ... and will autocomplete so that this fix is not actually required :)
In many cases, the 'additions' being loaded into the core would be much better handled in an IDE and already were in am ore flexible way ...
Except for the case when the default changes. With default you will to the new default, with a (once) generated code block in the IDE it will stay the old default.
And exactly this is the actual intention of this proposal (as far as I understood it).
--
Cheers
Jannik
Jannik Zschiesche wrote:
Quoted text · 4 lines
another small advantage would be that IDEs could show you the default value for the argument when hovering the default keyword. But a good IDE is already showing the full function and notes ... and will autocomplete so that this fix is not actually required :)
In many cases, the 'additions' being loaded into the core would be much better handled in an IDE and already were in am ore flexible way ...
Except for the case when the default changes. With
defaultyou will to the new default, with a (once) generated code block in the IDE it will stay the old default.And exactly this is the actual intention of this proposal (as far as I understood it).
? ... How would the default change from what is defined in the code? What am I missing?
--
Lester Caine - G8HFL
Contact - http://lsces.co.uk/wiki/?page=contact L.S.Caine Electronic Services - http://lsces.co.uk EnquirySolve - http://enquirysolve.com/
Model Engineers Digital Workshop - http://medw.co.uk Rainbow Digital Media - http://rainbowdigitalmedia.co.uk
Quoted text · 12 lines
Jannik Zschiesche wrote:
another small advantage would be that IDEs could show you the default
value
for the argument when hovering the default keyword.But a good IDE is already showing the full function and notes ... and will
autocomplete so that this fix is not actually required :)In many cases, the 'additions' being loaded into the core would be much better
handled in an IDE and already were in am ore flexible way ...Except for the case when the default changes. With
defaultyou will to the new default, with a (once) generated code block
in the IDE it will stay the old default.And exactly this is the actual intention of this proposal (as far as I understood it).
? ... How would the default change from what is defined in the code? What am I missing?
you write your code where you copypaste the default value some time passes
somebody changes the default value in the called function definition your code now calls the function with a non-default value.
--
Ferenc Kovács
@Tyr43l - http://tyrael.hu
Ferenc Kovacs wrote:
you write your code where you copypaste the default value some time passes
somebody changes the default value in the called function definition your code now calls the function with a non-default value.
Actually THAT is a problem I've hit in reverse! Some bugger changing the default in a library without understanding the consequences! In which case the fix was to put in the 'default' I wanted ... this works either way, but I could at least see in the IDE that the default had been changed so tracing it did not take as long as it could have.
--
Lester Caine - G8HFL
Contact - http://lsces.co.uk/wiki/?page=contact L.S.Caine Electronic Services - http://lsces.co.uk EnquirySolve - http://enquirysolve.com/
Model Engineers Digital Workshop - http://medw.co.uk Rainbow Digital Media - http://rainbowdigitalmedia.co.uk
2013/9/2 Lester Caine [email protected]
Quoted text · 4 lines
Ferenc Kovacs wrote:
you write your code where you copypaste the default value some time passes
somebody changes the default value in the called function definition your code now calls the function with a non-default value.Actually THAT is a problem I've hit in reverse! Some bugger changing the default in a library without understanding the consequences! In which case the fix was to put in the 'default' I wanted ... this works either way, but I could at least see in the IDE that the default had been changed so tracing it did not take as long as it could have.
But thats a problem no language (with default values for parameters of course) can help you, except you avoid default values in every case.
I for myself always use "null" as default value, so I must say, that a special keyword doesn't bring me any benefit. In this meaning: The named parameters are more useful :)
Regards,
Sebastian
Quoted text · 5 lines
--
Lester Caine - G8HFLContact - http://lsces.co.uk/wiki/?page=**contacthttp://lsces.co.uk/wiki/?page=contact L.S.Caine Electronic Services - http://lsces.co.uk EnquirySolve - http://enquirysolve.com/
Model Engineers Digital Workshop - http://medw.co.uk Rainbow Digital Media - http://rainbowdigitalmedia.co.**ukhttp://rainbowdigitalmedia.co.uk--
Hi!
another small advantage would be that IDEs could show you the default value for the argument when hovering the default keyword.
That actually would be excellent if they did it. I hope (if this is accepted) everybody does.
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
On Mon, Sep 2, 2013 at 9:17 AM, Stas Malyshev [email protected]wrote:
Quoted text · 10 lines
Hi!
I've finally took some time to revive the skipping parameter RFC and patch. For those who doesn't remember what it is please see: https://wiki.php.net/rfc/skipparams
TLDR version:The idea is to allow skipping parameters in function with optional arguments so that this:
function create_query($where, $order_by, $join_type='INNER', $execute = false, $report_errors = true)can be called like this:
create_query("deleted=0", "name", default, default, /report_errors/ true);Instead of trying to remember what the defaults are. The patch is here:
https://github.com/php/php-src/pull/426
Any comments or feedback on the RFCs and the code are welcome, especially pointing out the cases where it may not work (which means we need more phpt's there :)
I'm -1 on this proposal.
I think this doesn't really help readability. Right now you should implement functions with many options with an $options array.
If we want to change something here, we should skip this step and go right for named arguments. I think I'll give implementing them a try.
Nikita
Hi!
I think this doesn't really help readability. Right now you should implement functions with many options with an $options array.
I don't understand. Who says I should do that? I certainly don't see how I should.
If we want to change something here, we should skip this step and go right for named arguments. I think I'll give implementing them a try.
If you are ready to present named args proposal for 5.6, fine. But if not, I think denying obviously requested feature (see links for requests in the RFC) because some "pie in the sky" consideration is completely wrong.
I must say it seems a little strange for me given current stream of "because we can" syntax additions proposed to reject something that has been asked for by real people for years just because sometime in the future we might or might not have something that may do a similar thing in different way.
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
On Mon, Sep 2, 2013 at 2:11 PM, Stas Malyshev [email protected]wrote:
Quoted text · 9 lines
Hi!
I think this doesn't really help readability. Right now you should implement functions with many options with an $options array.
I don't understand. Who says I should do that? I certainly don't see how I should.
If we want to change something here, we should skip this step and go right for named arguments. I think I'll give implementing them a try.
If you are ready to present named args proposal for 5.6, fine. But if not, I think denying obviously requested feature (see links for requests in the RFC) because some "pie in the sky" consideration is completely wrong.
I must say it seems a little strange for me given current stream of "because we can" syntax additions proposed to reject something that has been asked for by real people for years just because sometime in the future we might or might not have something that may do a similar thing in different way.
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
I don't like it. Empty is how it's done on any language which allows this sort of thing, it's the most intuitive and I don't see anything wrong with it.
If you need this, it probably means that your priorities are not set well in the function. And if you have a function that has multiple optional parameters in which the order does not matter, you don't want multiple optional parameters, you want one optional array, which would contain whatever it is that you want.
Language constructs and syntax helpers aren't a substitute for well-written code and documentation.
On Mon, Sep 2, 2013 at 11:48 PM, Levi Morrison [email protected]wrote:
Oops, didn't mean to send a blank message, sorry all. Just clicked on the wrong spot.
Hi Stanislav
I would not agree with your argument that it should be introduced because it is requested by real people for years and it is simple to add. Isn't that pretty much the same as "because we can"? IMO we should wait with this RFC if Nikita is willing to write an RFC for named parameters including an implementation afterwards, because I think named parameters would address the problem way better. The skipping parameters as in your proposal would then just be the ugly alternative. However, if the RFC for named parameters will not be created or rejected, then I would appreciate your RFC.
Best,
Robert
-----Ursprüngliche Nachricht-----
Von: Stas Malyshev [mailto:[email protected]] Gesendet: Montag, 2. September 2013 22:12 An: Nikita Popov
Cc: PHP Internals
Betreff: Re: [PHP-DEV] [RFC] Skipping parameters take 2
Hi!
I think this doesn't really help readability. Right now you should implement functions with many options with an $options array.
I don't understand. Who says I should do that? I certainly don't see how I should.
If we want to change something here, we should skip this step and go right for named arguments. I think I'll give implementing them a try.
If you are ready to present named args proposal for 5.6, fine. But if not, I think denying obviously requested feature (see links for requests in the RFC) because some "pie in the sky" consideration is completely wrong.
I must say it seems a little strange for me given current stream of "because we can" syntax additions proposed to reject something that has been asked for by real people for years just because sometime in the future we might or might not have something that may do a similar thing in different way.
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
--
To unsubscribe, visit:
http://www.php.net/unsub.php
Hi!
I would not agree with your argument that it should be introduced because it is requested by real people for years and it is simple to add. Isn't that pretty much the same as "because we can"?
No, it is pretty much the opposite. It is "because people need it".
IMO we should wait with this RFC if Nikita is willing to write an RFC for named parameters including an implementation afterwards, because I think
We've been talking about it for years (last time here: https://wiki.php.net/rfc/namedparameters) but nothing happened. But I'm certainly willing to give it a chance, if it is happening. I just not want for all the effort to be wasted if it's not and we'd be left without a solution for a real problem again.
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
2013/9/2 Stas Malyshev [email protected]
Quoted text · 4 lines
Hi!
I would not agree with your argument that it should be introduced because it
is requested by real people for years and it is simple to add. Isn't that pretty much the same as "because we can"?No, it is pretty much the opposite. It is "because people need it".
I am a user and I don't need it and named parameters makes it obsolete anyway. Now it would be interesting, if "people" would "need" named parameters less, than the ability to pass "default" instead of "null" (or whatever)....
Quoted text · 6 lines
IMO we should wait with this RFC if Nikita is willing to write an RFC for named parameters including an implementation afterwards, because I think
We've been talking about it for years (last time here: https://wiki.php.net/rfc/namedparameters) but nothing happened. But I'm certainly willing to give it a chance, if it is happening. I just not want for all the effort to be wasted if it's not and we'd be left without a solution for a real problem again.
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227--
Quoted text · 4 lines
I must say it seems a little strange for me given current stream of "because we can" syntax additions proposed to reject something that has been asked for by real people for years just because sometime in the future we might or might not have something that may do a similar thing in different way.
I would not agree with your argument that it should be introduced because it
is requested by real people for years and it is simple to add. Isn't that pretty much the same as "because we can"?No, it is pretty much the opposite. It is "because people need it".
lolwut, you're kidding right? So you listed a bunch of StackOverflow questions asking for this and from this concluded that this is a feature "people need" whereas the RFCs by ircmaxell/me (whichever you mean) are not needed and are proposed just "because we can" and are not needed by "real people"? Would you like RFC's to include detailed chat logs, Twitter mentions and Reddit conversations as proof that features are wanted by "real people"?
Nikita
Quoted text · 7 lines
Hi!
I've finally took some time to revive the skipping parameter RFC and patch. For those who doesn't remember what it is please see: https://wiki.php.net/rfc/skipparams
TLDR version:The idea is to allow skipping parameters in function with optional arguments so that this:
function create_query($where, $order_by, $join_type='INNER', $execute = false, $report_errors = true)can be called like this:
create_query("deleted=0", "name", default, default, /report_errors/ true);
What would happen if I had done:
define('default', 42);
before that line?
cheers,
Derick
--
http://derickrethans.nl | http://xdebug.org Like Xdebug? Consider a donation: http://xdebug.org/donate.php twitter: @derickr and @xdebug
Posted with an email client that doesn't mangle email: alpine
On Mon, 2013-09-02 at 11:48 +0100, Derick Rethans wrote:
Quoted text · 5 lines
can be called like this:
create_query("deleted=0", "name", default, default, /report_errors/ true);What would happen if I had done:
define('default', 42);
before that line?
Nothing special as default is a keyword already (-> switch). Your only way to access that constant is already by using the constant() function.
johannes
Quoted text · 10 lines
Hi!
I've finally took some time to revive the skipping parameter RFC and patch. For those who doesn't remember what it is please see: https://wiki.php.net/rfc/skipparams
TLDR version:The idea is to allow skipping parameters in function with optional arguments so that this:
function create_query($where, $order_by, $join_type='INNER', $execute = false, $report_errors = true)can be called like this:
create_query("deleted=0", "name", default, default, /report_errors/ true);What would happen if I had done:
define('default', 42);before that line?
well, default is a keyword so you wouldn't be able to use it as a constant like that.
You would be able to define it via its string name, sure, but not access it directly.
Quoted text · 6 lines
cheers,
Derick--
http://derickrethans.nl | http://xdebug.org Like Xdebug? Consider a donation: http://xdebug.org/donate.php twitter: @derickr and @xdebug
Posted with an email client that doesn't mangle email: alpine--
--
Etienne Kneuss
Hi!
What would happen if I had done:
define('default', 42);
before that line?
Pretty much the same as if you did:
define('if', 42);
if($a == 1) print "one";
default is a keyword.
--
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
hi Stas,
Quoted text · 10 lines
Hi!
I've finally took some time to revive the skipping parameter RFC and patch. For those who doesn't remember what it is please see: https://wiki.php.net/rfc/skipparams
TLDR version:The idea is to allow skipping parameters in function with optional arguments so that this:
function create_query($where, $order_by, $join_type='INNER', $execute = false, $report_errors = true)can be called like this:
create_query("deleted=0", "name", default, default, /report_errors/ true);Instead of trying to remember what the defaults are. The patch is here:
https://github.com/php/php-src/pull/426
Any comments or feedback on the RFCs and the code are welcome, especially pointing out the cases where it may not work (which means we need more phpt's there :)
Using default instead of ,,, is indeed much more readable.
However I still wonder what prevents to finally implement named parameters too, it will provide the same feature while being even more handy and easier. I could dig the archives but I don't remember what was the reason why we rejected the idea back then.
Cheers,
Pierre
@pierrejoye | http://www.libgd.org
2013/9/2 Pierre Joye [email protected]
Quoted text · 14 lines
hi Stas,
On Mon, Sep 2, 2013 at 9:17 AM, Stas Malyshev [email protected] wrote:
Hi!
I've finally took some time to revive the skipping parameter RFC and patch. For those who doesn't remember what it is please see: https://wiki.php.net/rfc/skipparams
TLDR version:The idea is to allow skipping parameters in function with optional arguments so that this:
function create_query($where, $order_by, $join_type='INNER', $execute = false, $report_errors = true)can be called like this:
create_query("deleted=0", "name", default, default, /report_errors/ true);Instead of trying to remember what the defaults are. The patch is here:
https://github.com/php/php-src/pull/426
Any comments or feedback on the RFCs and the code are welcome, especially pointing out the cases where it may not work (which means we need more phpt's there :)
Using default instead of ,,, is indeed much more readable.
However I still wonder what prevents to finally implement named parameters too, it will provide the same feature while being even more handy and easier.
And it covers an additional use-case: Self-explaning parameters like in "foo(is_strict = false)" instead of "foo(null, null, false)".
Quoted text · 5 lines
I could dig the archives but I don't remember what was the reason why we rejected the idea back then.
Cheers,
Pierre
@pierrejoye | http://www.libgd.org
--
Quoted text · 5 lines
2013/9/2 Pierre Joye [email protected]
Any comments or feedback on the RFCs and the code are welcome, especially pointing out the cases where it may not work (which means we need more phpt's there :)
Using default instead of ,,, is indeed much more readable.
However I still wonder what prevents to finally implement named parameters too, it will provide the same feature while being even more handy and easier.
And it covers an additional use-case: Self-explaning parameters like in "foo(is_strict = false)" instead of "foo(null, null, false)".
Lots of overlap between variadic functions, this proposal & named parameters.
A popular use case for library authors is to use:
interface foo {
function formatUseCases(array $options); }
- Advantage: No dependency on a class / object
- Disadvantage: doesn't document what options are available, hard to extend 'default parameters'
interface foo {
function formatUseCases(MyOptions $options); }
- Advantage: documents which options are available, easy to extend default parameters
- Disadvantage: modification of the object means these 'options' are available to any declaration using it, hard to maintain over time without big refactoring (lots of options objects)
interface foo {
function formatUseCases(...$options);
}
- Advantage: No dependency on a class / object
- Disadvantage: doesn't document what options are available, no default parameters
interface foo {
function formatUseCases($showGood = true, $showBad = true, $pretty = true, $title= "what are these parameters?"); }
-
Advantage: Self-documenting with default parameters
-
Disadvantage: Not extendable api signature (changing default parameters)
-
Readability:
array
formatUseCases(['showGood': true, 'showBad': true, 'pretty': true]);
#object
$obj->showGood = true;
$obj->showBad = true;
$obj->pretty = true;
formatUseCases($obj);
variadic or function declaration
formatUseCases(true, true, true, "what are these parameters?");
- Solution somewhat as a hybrid?
interface foo {
function formatUseCases(...$options[showGood = true, showBad = true, pretty = true, title= "what are these parameters?"]); }
formatUseCases(true, true, true, "use defaults for everything else"); formatUseCases(['title': "use defaults for everything else"]); // more readable
Implemention wise $options could be ~ SplParameters which implements ArrayInterface :
class bar implements foo {
function formatUseCases(...$options[]) { // api signature as $options[] always accepted (uses default params) echo get_class($options); // SplParameters var_dump($options['showGood']) // true;
var_dump($options->showGood) // true;
}
}
class bar2 implements foo {
function formatUseCases(...$options[showGood = false]) { // easy to extend default options var_dump($options['showGood']) // false; }
}
Why use special syntax ~ "...$options[]" instead of just named parameters: http://msdn.microsoft.com/en-us/library/dd264739.aspx
My hunch is that applying named parameters to every function would be very costly in PHP. But as a special syntax applied to a few functions, this might work well.
2013/9/2 [email protected] [email protected]
Quoted text · 6 lines
2013/9/2 Pierre Joye [email protected]
Any comments or feedback on the RFCs and the code are welcome, especially pointing out the cases where it may not work (which means we need more phpt's there :)
Using default instead of ,,, is indeed much more readable.
However I still wonder what prevents to finally implement named parameters too, it will provide the same feature while being even more handy and easier.
And it covers an additional use-case: Self-explaning parameters like in "foo(is_strict = false)" instead of "foo(null, null, false)".
Lots of overlap between variadic functions, this proposal & named parameters.
I don't think that variadic functions prevent any of the other two, or that they are used for the same cases.
Quoted text · 5 lines
interface foo {
function formatUseCases(...$options);
}
- Advantage: No dependency on a class / object
- Disadvantage: doesn't document what options are available, no default parameters
This is totally not a use case for variadic functions. The arguments of a variadic function are indexed, not named. In addition, they have the same type (or at least they are treated the same way).
Lazare INEPOLOGLOU
Ingénieur Logiciel
Quoted text · 5 lines
interface foo {
function formatUseCases(...$options); }
- Advantage: No dependency on a class / object
- Disadvantage: doesn't document what options are available, no default parameters
This is totally not a use case for variadic functions. The arguments of a variadic function are indexed, not named. In addition, they have the same type (or at least they are treated the same way).
Right I'm for variadic '...$options' as proposed with indexed arguments (just like c). Python calls this positional arguments.
I'm stretching the comparison to see if syntax could be augmented to serve another use case (with named arguments)?
...$options[] would be more similar to **keyparam in python: http://rosettacode.org/wiki/Named_Arguments#Python
Hi!
However I still wonder what prevents to finally implement named parameters too, it will provide the same feature while being even more
For named params, you need to rewrite all args handling, since now it is a positional array and that doesn't work with named args. On the way you'd have to refactor zend_parse_parameters to understand named args (which aren't even named in many internals, so you'd have to add those) and deal with functions that use + and * in various creative ways and see how those can be reconciled with named params. And then of course is the question of by-ref parameters and how to make the syntax handle those too. Long story short, you'll have to rewrite everything that does with argument passing, argument receiving or even peeks into arguments.
Skipping parameters is relatively simple - you just put NULL and fix tons of functions which don't use defaults properly (will need to be done on a larger scale with named params too, because named params means you can skip args).
handy and easier. I could dig the archives but I don't remember what was the reason why we rejected the idea back then.
Bikeshedding about the syntax mostly, but that all pales compared to amount of work that needs to be done in the engine to support named params. Unless, of course, I'm completely wrong and there's an easy way to do it, which I am totally missing - in which case please point it out.
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
On Mon, Sep 2, 2013 at 10:19 PM, Stas Malyshev [email protected]wrote:
Hi!
However I still wonder what prevents to finally implement named parameters too, it will provide the same feature while being even more
For named params, you need to rewrite all args handling, since now it is a positional array and that doesn't work with named args. On the way you'd have to refactor zend_parse_parameters to understand named args (which aren't even named in many internals, so you'd have to add those) and deal with functions that use + and * in various creative ways and see how those can be reconciled with named params. And then of course is the question of by-ref parameters and how to make the syntax handle those too. Long story short, you'll have to rewrite everything that does with argument passing, argument receiving or even peeks into arguments.
I already have a mostly-working implementation for named arguments and unless I missed something critical it does not require us to redo argument passing. The idea I followed was to keep the argument passing pretty much as-is, just instead of always pushing on top of the stack to place named args directly at the correct offsets. So we still have a stack of positional arguments, they're just inserted out of order. This will also leave NULL values in between, so they need to be handled as in your implementation. The only thing you can't handle this way are named arguments that are not defined in the signature - if we want something like pythons **kwargs those need to be collected in an HT. I'd likely not support that at first, land basic named args instead and then seek to merge kwargs into the RFC's for variadics/unpacking.
Nikita
Hi!
I already have a mostly-working implementation for named arguments and unless I missed something critical it does not require us to redo argument passing. The idea I followed was to keep the argument passing pretty much as-is, just instead of always pushing on top of the stack to place named args directly at the correct offsets. So we still have a stack of positional arguments, they're just inserted out of order. This will also leave
NULLvalues in between, so they need to be handled as in your implementation. The only thing you can't handle this way are named
That may work and actually except for the syntax for default pretty much matches what my patch does completely. However what to do with internal functions, especially those using + and *? Not all of them have proper arginfos, and some of them would be pretty hard to describe properly - something like array_intersect_uassoc() for example. Current arginfo for it is completely misleading.
arguments that are not defined in the signature - if we want something like pythons **kwargs those need to be collected in an HT. I'd likely
This however is a question. **kwargs is super-ugly, and the fact that python has 2 sets of optional argument was always annoying for me. We should try to avoid it. Varargs syntax may actually be better there, but the question is where would we keep the names? Would additional hash table arg be on the stack? Wouldn't it clash with how proposed varargs work? Also, what about internals - how the engine knows names of the arguments there?
not support that at first, land basic named args instead and then seek to merge kwargs into the RFC's for variadics/unpacking.
Not supporting this means basically it doesn't solve the problem with options lists, which it is supposed to be the solution for. Also, it's not clear what func_get_args and all other inspection functions would do in such case. Of course, if you drop option lists and allow only pre-defined arguments the task becomes much easier, I agree, but also much less useful for things that **kwargs and such are used.
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
On Tue, Sep 3, 2013 at 12:00 AM, Stas Malyshev [email protected]wrote:
Hi!
I already have a mostly-working implementation for named arguments and unless I missed something critical it does not require us to redo argument passing. The idea I followed was to keep the argument passing pretty much as-is, just instead of always pushing on top of the stack to place named args directly at the correct offsets. So we still have a stack of positional arguments, they're just inserted out of order. This will also leave
NULLvalues in between, so they need to be handled as in your implementation. The only thing you can't handle this way are namedThat may work and actually except for the syntax for default pretty much matches what my patch does completely. However what to do with internal functions, especially those using + and *? Not all of them have proper arginfos, and some of them would be pretty hard to describe properly - something like
array_intersect_uassoc()for example. Current arginfo for it is completely misleading.
I think at some point you just need to go for "good enough" rather than "optimal support for everything". If we don't support the rather special case of internal functions having variadic arguments followed by fixed ones, I wouldn't consider that too tragic.
arguments that are not defined in the signature - if we want something like pythons **kwargs those need to be collected in an HT. I'd likely
This however is a question. **kwargs is super-ugly, and the fact that python has 2 sets of optional argument was always annoying for me. We should try to avoid it. Varargs syntax may actually be better there,
Yes, this is likely better in PHP's context. After all our array structure supports both continuous arrays (for normal varargs) and dictionaries (for kwargs), so I think we should be able to combine those.
but the question is where would we keep the names? Would additional hash table arg be on the stack?
Might be simpler to pass it via function_state.
Also, what about internals - how the engine knows names of the arguments there?
What do you mean by this?
not support that at first, land basic named args instead and then seek
to merge kwargs into the RFC's for variadics/unpacking.
Not supporting this means basically it doesn't solve the problem with options lists, which it is supposed to be the solution for. Also, it's not clear what func_get_args and all other inspection functions would do in such case. Of course, if you drop option lists and allow only pre-defined arguments the task becomes much easier, I agree, but also much less useful for things that **kwargs and such are used.
I think the main use of named args is in specifying existing arguments out of order. kwargs is a nice addition for minor use cases. I'll think about how to support those a bit more.
Nikita
Hi!
I think at some point you just need to go for "good enough" rather than "optimal support for everything". If we don't support the rather special
I am all for that. If only I wasn't this very minute bashed by several other people for not accounting for every exotic use case and not proposing optimal support for everything but just good enough for most practical use cases...
case of internal functions having variadic arguments followed by fixed ones, I wouldn't consider that too tragic.
It's not only this case, we have a number of internal functions that do weird things with their arguments. What "don't support" would mean in this case?
Also, what about internals - how the engine knows names of the arguments there?What do you mean by this?
Internal functions. Some of them have arginfos which bear only passing resemblance with what these functions actually do.
I think the main use of named args is in specifying existing arguments out of order. kwargs is a nice addition for minor use cases. I'll think about how to support those a bit more.
Option lists are everywhere, if you look at any framework everybody does it. Of course, now they do it with option arrays, and if we accept that option arrays are good then we don't need varargs either. But I thought the idea was that option arrays are not good enough actually.
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
Stas Malyshev wrote:
Option lists are everywhere, if you look at any framework everybody does it. Of course, now they do it with option arrays, and if we accept that option arrays are good then we don't need varargs either. But I thought the idea was that option arrays are not good enough actually.
Parameter hashes are what we have been converting everything TO because it was supposed to be the 'proper way to do it' a few years back. So when did they get kicked out again? Certainly passing a hash through a verify function before acting on it makes sense with all the code I'm using now ... the verify function fills in any default values and checks the rest of the past variables, and secondary objects create detail records. I'd hate to be doing this nowadays with lists of parameters. It just would not work.
--
Lester Caine - G8HFL
Contact - http://lsces.co.uk/wiki/?page=contact L.S.Caine Electronic Services - http://lsces.co.uk EnquirySolve - http://enquirysolve.com/
Model Engineers Digital Workshop - http://medw.co.uk Rainbow Digital Media - http://rainbowdigitalmedia.co.uk
Parameter hashes are what we have been converting everything TO because it was supposed to be the 'proper way to do it' a few years back.
If you have lots of parameters to pass in, the better solution is to use an object, which lets you formally define what is required to be passed in. The parameter hash, or the parameter block as it was known in decades past, is a compromise solution that was widely used before OOP really caught on. In this context, it is essentially the same thing as a simple object but without the formal definition.
The lack of formal definition is what makes it a terrible idea because it obfuscates the parameters of the function. If you hand off the code to someone else to use, they must to look at the implementation to see how it works to figure out what to pass in. Oh, there may be separate docs, but they can't be auto-generated, and we all know how well code docs are kept up-to-date when they're not auto-generated. Plus, PHPDoc doesn't support parameter blocks, which means that IDEs can't offer the same level of assistance with code-completion that they offer for both objects and straight parameters -- another huge downside.
Parameters were invented as an abstraction around passing raw, untyped and unnamed stacks of data. Parameter blocks take us back to that.
Back to the topic, I like what Stas has proposed. Further, I don't see named parameters as replacing the utility of default parameters. For long parameter lists, named parameters would usually make more sense to use, I think, but for medium lists, I think the default keyword is much cleaner because it doesn't require doubling or tripling the length of what you type for the parameter list to ensure you're getting the default value for a parameter. And where named parameters are overkill, it keeps the focus of anyone reading the code firmly on the values, not the parameter names. In other words, the two features complement each other, with one or the other being better in different contexts.
I don't get a vote, but if I did, I'd say implement what Stas has put forth, and if named parameters can come into the picture at some point, implement that, too.
--
Bob Williams
Notice: This communication, including attachments, may contain information that is confidential. It constitutes non-public information intended to be conveyed only to the designated recipient(s). If the reader or recipient of this communication is not the intended recipient, an employee or agent of the intended recipient who is responsible for delivering it to the intended recipient, or if you believe that you have received this communication in error, please notify the sender immediately by return e-mail and promptly delete this e-mail, including attachments without reading or saving them in any manner. The unauthorized use, dissemination, distribution, or reproduction of this e-mail, including attachments, is prohibited and may be unlawful. If you have received this email in error, please notify us immediately by e-mail or telephone and delete the e-mail and the attachments (if any).
Robert Williams wrote:
PHPDoc doesn't support parameter blocks, which means that IDEs can't offer the same level of assistance with code-completion that they offer for both objects and straight parameters -- another huge downside.
PHPDoc's does 'not' not support parameter blocks ... you just document them properly in the first place. Switching these to formalised objects introduces another level of complexity that detracts from their use rather than enhancing it, but again that is more to do with maintaining BC. Something that has become a second class citizen nowadays?
Much of the work I've been wasting time on over the past years is re-factoring legacy code to make it E_STRICT compliant. Switching TO using arrays has been part of that exercise and was helpful in getting around the warnings raised to the original code. Much of the material I'm working with is arrays of data into and out of a database, so not naturally an 'object'?
OK adding 'default' or going on to finally force named parameters on us is not going to break anything but it's creating yet more new styles of coding when what we NEED is to simplify and get back to a style of working that we can agree on and start producing applications rather than reinventing the engine every year :(
Currently - as I've said many times - there is no 'style guide' to provide a best practice we can work to. PSR is probably the closest we have but even that is a clique of users with a particular agenda which is why it's not formally accepted by the general development team? Redoing everything again to conform with that would be another year or so's work for me :(
Coding used to be fun - now it's just a chore :(
--
Lester Caine - G8HFL
Contact - http://lsces.co.uk/wiki/?page=contact L.S.Caine Electronic Services - http://lsces.co.uk EnquirySolve - http://enquirysolve.com/
Model Engineers Digital Workshop - http://medw.co.uk Rainbow Digital Media - http://rainbowdigitalmedia.co.uk
Robert Williams wrote:
PHPDoc doesn't support parameter blocks, which means that IDEs can't offer the same level of assistance with code-completion that they offer for both objects and straight parameters -- another huge downside.
PHPDoc's does 'not' not support parameter blocks ... you just document them properly in the first place. Switching these to formalised objects introduces another level of complexity that detracts from their use rather than enhancing it, but again that is more to do with maintaining BC. Something that has become a second class citizen nowadays?
PSR-5 (draft) does support them:
https://github.com/phpDocumentor/fig-standards/blob/master/proposed/phpdoc.md#7-describing-hashes
--
Alexey Zakhlestin,
http://github.com/indeyets
Alexey Zakhlestin wrote:
Quoted text · 4 lines
PHPDoc doesn't support parameter blocks, which means that IDEs can't offer
the same level of assistance with code-completion that they offer for both objects and straight parameters -- another huge downside.
PHPDoc's does 'not' not support parameter blocks ... you just document them properly in the first place. Switching these to formalised objects introduces another level of complexity that detracts from their use rather than enhancing it, but again that is more to do with maintaining BC. Something that has become a second class citizen nowadays? PSR-5 (draft) does support them:
https://github.com/phpDocumentor/fig-standards/blob/master/proposed/phpdoc.md#7-describing-hashes
Interesting ... pity the current phpdocumentor versions can't support our 'legacy' style of working :( I'm still stuck with 1.4.2 to create http://lsces.org.uk/bitweaverdocsPHP/fisheye/FisheyeGallery.html
--
Lester Caine - G8HFL
Contact - http://lsces.co.uk/wiki/?page=contact L.S.Caine Electronic Services - http://lsces.co.uk EnquirySolve - http://enquirysolve.com/
Model Engineers Digital Workshop - http://medw.co.uk Rainbow Digital Media - http://rainbowdigitalmedia.co.uk
Hi Stas,
I really like this RFC. It makes it simple to use defined defaults without the need to know about them of to updated.
But I think adding "default" as new keyword is a big BC break! I personally also don't like it and asked myself why can't the parameter simply skipped?
function foo ($a='a', $b='b') {}
foo();
foo($a);
foo(, $b);
foo($a,);
foo(,);
Is it possible to use the default parameter on inheritance?
class Bar {
function foo($a='a', $b='b') {}
}
class Baz extends Bar {
function foo($a=default, $b=default) {
// do something
parent::foo($a, $b);
}
}
Marc
Am 02.09.2013 um 09:17 schrieb Stas Malyshev:
Quoted text · 10 lines
Hi!
I've finally took some time to revive the skipping parameter RFC and patch. For those who doesn't remember what it is please see: https://wiki.php.net/rfc/skipparams
TLDR version:The idea is to allow skipping parameters in function with optional arguments so that this:
function create_query($where, $order_by, $join_type='INNER', $execute = false, $report_errors = true)can be called like this:
create_query("deleted=0", "name", default, default, /report_errors/ true);Instead of trying to remember what the defaults are. The patch is here:
https://github.com/php/php-src/pull/426
Any comments or feedback on the RFCs and the code are welcome, especially pointing out the cases where it may not work (which means we need more phpt's there :)
But I think adding "default" as new keyword is a big BC break!
Default already is a keyword: http://php.net/switch. There's no BC break.
I personally also don't like it and asked myself why can't the parameter simply skipped?
That was in the original proposal, but counting commas is pretty lousy if you're skipping more than one or two parameters. Having a keyword makes it more readable.
Adam
Am 14.01.2015 um 20:21 schrieb Adam Harvey:
But I think adding "default" as new keyword is a big BC break! Default already is a keyword: http://php.net/switch. There's no BC break.
OMG you are right - my fault
I personally also don't like it and asked myself why can't the parameter simply skipped?
That was in the original proposal, but counting commas is pretty lousy if you're skipping more than one or two parameters. Having a keyword makes it more readable.Adam
Hi,
-1 on this proposal
+1 on named parameters
As of for this...
handy and easier. I could dig the archives but I don't remember what was the reason why we rejected the idea back then.
Bikeshedding about the syntax mostly, but that all pales compared to amount of work that needs to be done in the engine to support named params. Unless, of course, I'm completely wrong and there's an easy way to do it, which I am totally missing - in which case please point it out.
Pierrick and I both implemented this support for Annotations back in 2010. Maybe it's worth to look into that patch for some ideas.
Regards,
Quoted text · 9 lines
Am 14.01.2015 um 20:21 schrieb Adam Harvey:
But I think adding "default" as new keyword is a big BC break!
Default already is a keyword: http://php.net/switch. There's no BC break.
OMG you are right - my fault
I personally also don't like it and asked myself why can't the parameter
simply skipped?
That was in the original proposal, but counting commas is pretty lousy if you're skipping more than one or two parameters. Having a keyword makes it more readable.
Adam
--
--
Guilherme Blanco
MSN: [email protected]
GTalk: guilhermeblanco
Toronto - ON/Canada
Hi!
-1 on this proposal
+1 on named parameters
Come on, we've already talked about it like 20 times and it has special paragraph in the RFC dedicated exactly to this. It's not instead named params, we can do both.
Pierrick and I both implemented this support for Annotations back in 2010. Maybe it's worth to look into that patch for some ideas.
Annotations are great and I'd like them to be resurrected (provided that we don't get bogged down again with "let's implement a Turing-complete DSL for for ORMs inside annotations because we have like 1 or even 2 use cases for it!") but how it is relevant to the topic or to named params? That said, if you want to branch the topic and discuss it it's fine, just please change the subj.
Stas Malyshev
[email protected]
Hi Stas,
As I said, we should look at that patch as we implemented Named Parameters there with everything you mentioned.
Cheers,
On Wed, Jan 14, 2015 at 3:23 PM, Stanislav Malyshev [email protected] wrote:
Quoted text · 10 lines
Hi!
-1 on this proposal
+1 on named parameters
Come on, we've already talked about it like 20 times and it has special paragraph in the RFC dedicated exactly to this. It's not instead named params, we can do both.
Pierrick and I both implemented this support for Annotations back in
Maybe it's worth to look into that patch for some ideas.
Annotations are great and I'd like them to be resurrected (provided that we don't get bogged down again with "let's implement a Turing-complete DSL for for ORMs inside annotations because we have like 1 or even 2 use cases for it!") but how it is relevant to the topic or to named params? That said, if you want to branch the topic and discuss it it's fine, just please change the subj.
Stas Malyshev
[email protected]
--
Guilherme Blanco
MSN: [email protected]
GTalk: guilhermeblanco
Toronto - ON/Canada
Marc Bennewitz, Stas,
But I think adding "default" as new keyword is a big BC break! I personally also don't like it and asked myself why can't the parameter simply skipped?
Default is already a reserved word AFAIK. But I've been thinking about alternatives to parameter skipping syntax which brought me the Golang blank identifier _ to mind:
create_query("deleted=0", "name", _, _, true);
Still not sure if it's better than default, though.
2015-01-14 16:15 GMT-03:00 Marc Bennewitz [email protected]:
Quoted text · 26 lines
Hi Stas,
I really like this RFC. It makes it simple to use defined defaults without the need to know about them of to updated.
But I think adding "default" as new keyword is a big BC break! I personally also don't like it and asked myself why can't the parameter simply skipped?
function foo ($a='a', $b='b') {} foo(); foo($a); foo(, $b); foo($a,); foo(,);Is it possible to use the default parameter on inheritance?
class Bar {
function foo($a='a', $b='b') {}
}class Baz extends Bar {
function foo($a=default, $b=default) {
// do something
parent::foo($a, $b);
}
}Marc
Am 02.09.2013 um 09:17 schrieb Stas Malyshev:
Hi!
I've finally took some time to revive the skipping parameter RFC and patch. For those who doesn't remember what it is please see: https://wiki.php.net/rfc/skipparams
TLDR version:The idea is to allow skipping parameters in function with optional arguments so that this:
function create_query($where, $order_by, $join_type='INNER', $execute = false, $report_errors = true)can be called like this:
create_query("deleted=0", "name", default, default, /report_errors/ true);Instead of trying to remember what the defaults are. The patch is here:
https://github.com/php/php-src/pull/426
Any comments or feedback on the RFCs and the code are welcome, especially pointing out the cases where it may not work (which means we need more phpt's there :)
Quoted text · 5 lines
Marc Bennewitz, Stas,
But I think adding "default" as new keyword is a big BC break! I personally also don't like it and asked myself why can't the parameter simply skipped?
Default is already a reserved word AFAIK. But I've been thinking about alternatives to parameter skipping syntax which brought me the Golang blank identifier _ to mind:
create_query("deleted=0", "name", _, _, true);
Still not sure if it's better than
default, though.
That would be a BC break as you can currently have a constant _. An existing keyword makes upgrading easier, and default is easier to understand by someone who doesn’t know about the feature than an underscore.
Simon Welsh
Admin of http://91carriage.com/ – Specialised SilverStripe hosting
Am 14.01.2015 20:50 schrieb "Simon J Welsh" [email protected]:
create_query("deleted=0", "name", _, _, true);
Still not sure if it's better than
default, though.That would be a BC break as you can currently have a constant _.
A visually pleasing (IMO), even easier to type, and non-BC alternative, could be using a dash:
create_query("deleted=0", "name", -, -, true);
best regards
Patrick
Hi all,
Quoted text · 6 lines
Am 14.01.2015 20:50 schrieb "Simon J Welsh" [email protected]:
create_query("deleted=0", "name", _, _, true);
Still not sure if it's better than
default, though.That would be a BC break as you can currently have a constant _.
A visually pleasing (IMO), even easier to type, and non-BC alternative, could be using a dash:
create_query("deleted=0", "name", -, -, true);
Cryptic notation is not a PHP way. IMHO. I like original "default" proposal and it is good enough.
Regards,
--
Yasuo Ohgaki
[email protected]
Cryptic notation is not a PHP way. IMHO.I like original "default" proposal and it is good enough. Regards,
When I suggested _ it was more as a feature wandering. I like default too. The RFC looks good enough as it is now :)
2015-01-17 22:14 GMT-03:00 Yasuo Ohgaki [email protected]:
Quoted text · 12 lines
Hi all,
Am 14.01.2015 20:50 schrieb "Simon J Welsh" [email protected]:
create_query("deleted=0", "name", _, _, true);
Still not sure if it's better than
default, though.That would be a BC break as you can currently have a constant _.
A visually pleasing (IMO), even easier to type, and non-BC alternative, could be using a dash:
create_query("deleted=0", "name", -, -, true);
Cryptic notation is not a PHP way. IMHO. I like original "default" proposal and it is good enough.
Regards,
--
Yasuo Ohgaki
[email protected]
Just a note:
I still think a single char token (like ~) would look great and wouldn't be "cryptic" like Yasuo argued. But, as said before, it's just a minor observation. The default token is a great option and sounds very intuitive to anyone I've asked for opinions.
2015-01-17 22:52 GMT-03:00 Marcio Almada [email protected]:
Quoted text · 15 lines
Cryptic notation is not a PHP way. IMHO.I like original "default" proposal and it is good enough. Regards,
When I suggested
_it was more as a feature wandering. I likedefaulttoo. The RFC looks good enough as it is now :)2015-01-17 22:14 GMT-03:00 Yasuo Ohgaki [email protected]:
Hi all,
Am 14.01.2015 20:50 schrieb "Simon J Welsh" [email protected]:
create_query("deleted=0", "name", _, _, true);
Still not sure if it's better than
default, though.That would be a BC break as you can currently have a constant _.
A visually pleasing (IMO), even easier to type, and non-BC alternative, could be using a dash:
create_query("deleted=0", "name", -, -, true);
Cryptic notation is not a PHP way. IMHO. I like original "default" proposal and it is good enough.
Regards,
--
Yasuo Ohgaki
[email protected]
Hi Marcio,
Just a note:
I still think a single char token (like
~) would look great and wouldn't be "cryptic" like Yasuo argued. But, as said before, it's just a minor observation. Thedefaulttoken is a great option and sounds very intuitive to anyone I've asked for opinions.
For consistency with list(), we could also just put nothing:
foo($bar, , $baz);
Which is like:
list($bar, , $baz) = $arr;
Thoughts?
Andrea Faulds
http://ajf.me/
Andrea,
Quoted text · 5 lines
For consistency with list(), we could also just put nothing:
foo($bar, , $baz);
Which is like:
list($bar, , $baz) = $arr;
Thoughts?
Not sure. Do we consider both contexts (list assignment skipping and parameter skipping) as assignments? If so, then the consistency arguments seems very strong to me.
Only point against the consistency path is that in the context of list() the blank identifier is used to discard a value, while on parameter skipping the blank identifier inherits a previously declared value.
Hey Marcio,
Quoted text · 7 lines
Andrea,
For consistency with list(), we could also just put nothing:
foo($bar, , $baz);
Which is like:
list($bar, , $baz) = $arr;
Thoughts?
Not sure. Do we consider both contexts (list assignment skipping and parameter skipping) as assignments? If so, then the consistency arguments seems very strong to me.
Not really. But, list() is very function-like in syntax, so it’d make sense to follow its lead for actual functions.
Only point against the consistency path is that in the context of list() the blank identifier is used to discard a value, while on parameter skipping the blank identifier inherits a previously declared value.
Yes, I suppose that’s a fair point.
Though if you were to implement a list() clone as a function (purely for the sake of example, you wouldn’t actually want to do this), you might actually do something similar:
function listLike(&$a = 0, &$b = 0, &$c = 0, array $arr) {
if ($a !== 0) {
$a = $arr[0];
}
if ($b !== 0) {
$b = $arr[1];
}
if ($c !== 0) {
$c = $arr[2];
}
}
listLike($bar, 0, $baz, $arr);
list()’s value skipping is a little bit like function argument skipping, in that way, right?
With this list()-like default value syntax, it’d even work the same:
listLike($bar, , $baz, $arr);
…that probably made no sense. But I think there’s a case to be made that since list() follows this syntax, we should for function calls to.
By the way, while I was opposed to this RFC before, I am seeing its value somewhat when using badly-designed APIs like json_decode. That said, I’m still not sure I like the RFC concept… better to make new, improved APIs than add kludges to make bad APIs more usable.
--
Andrea Faulds
http://ajf.me/
Quoted text · 5 lines
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. :)
Am 19.01.2015 um 19:48 schrieb Adam Harvey:
Quoted text · 8 lines
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.
@Stas: Any news on using default on inheritance ?
Marc
Hi!
@Stas: Any news on using default on inheritance ?
Not yet, probably will look into this towards next weekend.
--
Stas Malyshev
[email protected]
Quoted text · 33 lines
Am 19.01.2015 um 19:48 schrieb Adam Harvey:
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 bylistcon:
- hard to read and to differ from mistakes - not an option for inheritanceUsing the keyword
default:
pro:
- better to read and to differ from mistakes - usable on inheritance
con:
- different syntax aslist
- not possible as alternative syntax forlistas it has no "default" namingUsing a special character:
pro:
- better to read and to differ from mistakes - usable on inheritance
- possibly an alternative syntax forlist(for consistency) con:
- different syntax aslist
- Not a good character found for it, yetNamed 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 complexityIn 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]
Quoted text · 6 lines
Pros & Cons
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
Nice summary, but I think "Adds variable names to be part of the API" is such a big thing it deserves two lines, as it would be horrible for things like anonymous functions or callbacks. e.g.
<?php
$f = function ($foo = 'first', $bar = 'second') { return $foo."_".$bar."\n";
};
$g = function ($bar = 'first' , $foo = 'second') { return $bar."_".$foo."\n";
};
if (rand(0,2)) {
$h = $f;
}
else {
$h = $g;
}
// Both output 'first_second'
echo $h('first', 'second');
echo $h('first');
// 'default' placeholder outputs 'first_second' echo $h(default, 'second');
// Named param outputs 'second_second' some of the time. echo $h(bar => 'second');
?>
Although named params sound like a nice idea, looking at how they would act in a large code base, it seems like they would be a source of unexpected issues, when people make assumptions about a function based on the argument names. People would probably have to mark libraries as safe to call via named params or not, which doesn't sound good.
I'm pretty sure that parameter order is what actually defines a function's signature and so the 'default' placeholder is the only option that makes sense.
cheers
Dan
Cryptic notation is not a PHP way. IMHO.I like original "default" proposal and it is good enough. Regards,
When I suggested
_it was more as a feature wandering. I likedefaulttoo. The RFC looks good enough as it is now :)
_ is not really an option since it is the standard gettext() shortcut.
Eg.
http://en.m.wikipedia.org/wiki/Gettext#Programming http://php.net/_
-Rasmus
Rasmus,
_ is not really an option since it is the standard
gettext()shortcut.
Yup, Simon J. pointed that out too so usage of _ was already a discarded topic.
The top topic now is a possible consistency with the syntax already allowed when using list. Ex:
list($bar, , $baz) = ['good', 'trash', 'nice'];
Andrea,
…that probably made no sense. But I think there’s a case to be made that since list() follows this syntax, we should for function calls to.
But I get it, it would also prevent language syntax from growing just to have a trivial feature. Apart from the debate about meaning of the assignment using a blank identifier, it would be beneficial to use the same syntax even though situations are distinct.
I guess only Stas can decide to give up on default and use a blank identifier. Anyway, hope this decision don't affect the voting negatively, feature is cool.
2015-01-18 0:32 GMT-03:00 Rasmus Lerdorf [email protected]:
Quoted text · 7 lines
Cryptic notation is not a PHP way. IMHO.I like original "default" proposal and it is good enough.
Regards,When I suggested
_it was more as a feature wandering. I likedefaulttoo. The RFC looks good enough as it is now :)_ is not really an option since it is the standard
gettext()shortcut.Eg.
http://en.m.wikipedia.org/wiki/Gettext#Programming http://php.net/_-Rasmus
Hi!
Quoted text · 10 lines
Is it possible to use the default parameter on inheritance?
class Bar {
function foo($a='a', $b='b') {}
}class Baz extends Bar {
function foo($a=default, $b=default) {
// do something
parent::foo($a, $b);
}
}
It's not part of the original proposal, and I'm not sure how easy it would be to implement, but it sounds like a nice extension, I didn't think about it. Since the RFC is already in vote, I won't change it now, but I'll look into if it's possible to do it, and if the RFC is accepted, and it proves possible, I'll propose it separately.
--
Stas Malyshev
[email protected]
Hi!
It's not part of the original proposal, and I'm not sure how easy it would be to implement, but it sounds like a nice extension, I didn't think about it. Since the RFC is already in vote, I won't change it now,
Strike that, I was confused, for some reason I thought I've already put it to vote, even though I've put to vote my other RFC (shameless plug: https://wiki.php.net/rfc/default_ctor, go there and vote Yes ;). Too many RFCs :) So I'll think if it's easy to add and maybe add it before it goes to vote.
--
Stas Malyshev
[email protected]
Hi Stas,
Quoted text · 18 lines
-----Ursprüngliche Nachricht-----
Von: Stanislav Malyshev [mailto:[email protected]] Gesendet: Mittwoch, 14. Januar 2015 21:26 An: Marc Bennewitz; [email protected] Betreff: Re: [PHP-DEV] [RFC] Skipping parameters take 2Hi!
Is it possible to use the default parameter on inheritance?
class Bar {
function foo($a='a', $b='b') {}
}class Baz extends Bar {
function foo($a=default, $b=default) {
// do something
parent::foo($a, $b);
}
}It's not part of the original proposal, and I'm not sure how easy it would be to implement, but it sounds like a nice extension, I didn't think about it. Since the RFC is already in vote, I won't change it now, but I'll look into if it's possible to do
it, and if the RFC is accepted, and it proves possible, I'll propose it separately.--
Stas Malyshev
[email protected]
This would be quite a nice feature, even if this RFC does not pass. Just as hint, there are ambiguous case which need to be considered:
interface A{
function foo($a=1);
}
interface B{
function foo($a="hi");
}
class C implements A, B{
function foo($a=default){} //what would be the default value? }
Cheers,
Robert
Hi!
This would be quite a nice feature, even if this RFC does not pass. Just as hint, there are ambiguous case which need to be considered:
interface A{ function foo($a=1); } interface B{ function foo($a="hi"); } class C implements A, B{ function foo($a=default){} //what would be the default value? }
I'm not sure specifying the default on the interface makes much sense, since you'd have to override it anyway. I am inclined to only support it for extending (parent class). But if there's an easy way to do it for interfaces, it's ok - but checking that there are no duplicates may be expensive.
--
Stas Malyshev
[email protected]
Quoted text · 6 lines
-----Ursprüngliche Nachricht-----
Von: Stanislav Malyshev [mailto:[email protected]] Gesendet: Freitag, 16. Januar 2015 21:35 An: Robert Stoll; 'Marc Bennewitz'; [email protected] Betreff: Re: AW: [PHP-DEV] [RFC] Skipping parameters take 2Hi!
This would be quite a nice feature, even if this RFC does not pass. Just as hint, there are ambiguous case which need to be considered:
interface A{ function foo($a=1); } interface B{ function foo($a="hi"); } class C implements A, B{ function foo($a=default){} //what would be the default value? }
I'm not sure specifying the default on the interface makes much sense, since you'd have to override it anyway. I am inclined to only support it for extending (parent class). But if there's an easy way to do it for interfaces, it's ok
- but checking
Quoted text · 5 lines
that there are no duplicates may be expensive.
--
Stas Malyshev
[email protected]--
I think you are right and restricting it to the parent class only would remove the ambiguity
Hi!
Quoted text · 6 lines
class Baz extends Bar {
function foo($a=default, $b=default) {
// do something
parent::foo($a, $b);
}
}
I tried to implement this, however there's a major issue that I do not know how to overcome. The issue is that when we compile method foo() we do not know the default values. These values are stored in RECV_INIT opcodes directly, so when we find out what the actual values are - i.e. when we resolve inheritance for class Baz - it may be in runtime, so we'd have to update opcodes for RECV_INIT in foo() in runtime. Which is not going to sit well with opcode cache, which assumes opcodes do not change.
I'm not sure yet how to solve this - since the values are only stored in the opcodes it is not easy to locate them in runtime, so even if we make 'default' zvals some special type we'd still have a problem finding the right value since the binding happens in runtime and we don't have now any place to store the result of that binding.
I'll think a bit more about it, in the meantime any ideas about how to resolve this problem are welcome.
--
Stas Malyshev
[email protected]