Re: array coalesce operator concept
| From: | Tony Marston | Date: | Thu, 13 Jul 2017 09:55:44 +0000 |
| Subject: | Re: array coalesce operator concept | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-99869@lists.php.net to get a copy of this message | ||
"David Rodrigues" wrote in message news:CAEsg9X2ECG62i7Z_Nu11kqr7yVBkmUcd3MxGt78ULAmpFx--_w@mail.gmail.com...
The idea is good, but I don't know if it is more clear than an 'if()'I think that this proposal is totally ridiculous and should be shot down in flames. My reasoning is as follows: 1) There should be a rule that anything which can be achieved with 5 lines or less in userland code should be automatically excluded from becoming a change to the language. The cost of changing the language in order to get around the laziness of a small number of users versus the "benefit" of making the language more complicated for the rest of us, as well as adding to the maintenance burden is simply not worth it. 2) Proposals such as this violate the first rule of good programming which is to provide code which is readable and therefore understandable by others, and therefore maintainable. I don't know about you, but I find it much easier to read words than symbols. The idea that the code "if (is_array($foo))" is too verbose and should be replaced with a shorthand symbol is just too ridiculous for word. If any more of these shortcuts is implemented then I'm afraid that code will look too much like a bunch of hieroglyphics and become far less readable. Just my 2 cents worth. -- Tony Marston >Em 11 de jul de 2017 12:15 PM, "Mark Shust" <mark@shust.com> escreveu: > >> Hello, >> >> I wanted to garnish feedback on a RFC proposal. This is just a concept at >> this point, and is inspired by the null coalesce operator. >> >> Code will error if a non-array value is passed through a looping feature. >> For example, this code: >> >> <?php >> >> $foo = "abc"; >> >> foreach ($foo as $bar) { >> >> echo $bar; >> >> } >> >> >> will result in the error: >> >> PHP Warning: Invalid argument supplied for foreach() in test.php on line >> 3 >> >> >> To prevent this, the logical solution is to wrap this in an is_array >> check: >> >> <?php >> >> $foo = "abc"; >> >> if (is_array($foo)) { >> >> foreach ($foo as $bar) { >> >> echo $bar; >> >> } >> >> } >> >> >> This code runs successfully and does not error out. For a syntactic >> sugar/improvement, this can be shorthand for executing the loop instead >> of >> wrapping the block within an is_array check: >> >> >> <?php >> >> $foo = "abc"; >> >> foreach (??$foo as $bar) { >> >> echo $bar; >> >> } >> >> >> Let me know your thoughts. >> >> >> Cheers, >> Mark >>