Proposal: Dangling comma in function call parameters

php.internals

Christian Schneider

22 years ago
Hello everybody, just joined the internals list (after being absent from php developers lists for a while) so excuse my ignorance if any topic I bring up has been discussed already. I checked the archives but couldn't find anything... Proposal: Allow dangling commas in function call parameters. Reasoning: We use some handy vararg tool functions and it's nice to be able to write foo( 1, 2, 3, ); since it's then easy to add a new parameter without having to add a comma to the previous list. This would be symmetrical to the array() syntax. The patch for zend (php4 from cvs) would be minimal and I can't see any backwards-compatibility issues right now (patch attached). Any opinions? Regards, - Chris

Andi Gutmans

22 years ago
At 11:32 PM 10/22/2003 +0200, Christian Schneider wrote:
>Hello everybody, >just joined the internals list (after being absent from php developers >lists for a while) so excuse my ignorance if any topic I bring up has been >discussed already. I checked the archives but couldn't find anything... > >Proposal: Allow dangling commas in function call parameters. >Reasoning: We use some handy vararg tool functions and it's nice to be >able to write >foo( > 1, > 2, > 3, >); >since it's then easy to add a new parameter without having to add a comma >to the previous list. > >This would be symmetrical to the array() syntax. The patch for zend (php4 >from cvs) would be minimal and I can't see any backwards-compatibility >issues right now (patch attached). > >Any opinions?
The only reason array() supports this is because there are some cases where people auto-generate initialization files which have a huge array() construct and it makes it slightly easier (don't have the last element problem). In this case I don't see the same advantage and I see a disadvantage in readability and the possibility for PHP to give such an empty argument a meaning in future (although I doubt that'll happen). So -1 from me. Andi

Christian Schneider

22 years ago
Andi Gutmans wrote:
> In this case I don't see the same advantage and I see a disadvantage in > readability and the possibility for PHP to give such an empty argument a > meaning in future (although I doubt that'll happen).
Ok, here the real-world example from our HTML generation toolkit: table( tr(td('row 1 cell 1'), td('row 1 cell 2')), tr(td('row 2 cell 1'), td('row 2 cell 2')), ); Being able to easily append lines here would be really nice :-) It doesn't break existing things _and_ it's up to the programmer to use this feature, same as in array(). I was also thinking of proposing named parameters, i.e. basically removing the need for array() in foo(array('style' => 'hot', 'size' => 42)); but I guess that'll be even more controversial :-) Ok, that's it from my side, it's up to you now, - Chris

netcat

22 years ago
Christian Schneider wrote: [snip]
> > I was also thinking of proposing named parameters, i.e. basically > removing the need for array() in foo(array('style' => 'hot', 'size' => > 42)); but I guess that'll be even more controversial :-)
Named parameters - i think is very good idea. I know i would use them. I'm really not sure about the correct syntax for it though. They should be optional and may have default values and provide a way to extend old functions that already have optional parameters. Just for example: function f($a,$b=false,keyword $c='xyz') { } f(1) f(1,true) f(1,c='www') f(1,true,c='www') Just general thoughts: Maybe there should be a way to specify that a keyword is required. Maybe there should be a way to specify external name other than argument variable name as in the following example: function g($a,keyword 'aa' $ab) { }
> > > Ok, that's it from my side, it's up to you now, > - Chris >
-- NetCat ------------------------------------------------------ SPAM-Free 10mb Free email + Antivirus + POP3 + more... Get it at http://www.doal.co.il:81/free/?c=all-spam

Christian Schneider

22 years ago
netcat wrote:
> Named parameters - i think is very good idea. I know i would use them. > I'm really not sure about the correct syntax for it though.
Important for our application would be that it works with varargs, as we have a lot of (potential) parameters of which you normally only give a small subset. And having a huge parameter list with default values just doesn't do it there. We're using one associative array as parameter for this purpose right now. - Chris

Sebastian Bergmann

22 years ago
netcat wrote:
> Named parameters - i think is very good idea.
Named parameters are commonly implemented using an associated array in PHP: <?php function foo($parameters) {} foo( array( 'foo' => 'bar', 'bar' => 'foo' ) ); ?>
-- Sebastian Bergmann http://sebastian-bergmann.de/ http://phpOpenTracker.de/ Das Buch zu PHP 5: http://professionelle-softwareentwicklung-mit-php5.de/