Re: Strict switch statements
| From: | Chase Peeler | Date: | Thu, 14 Jun 2018 21:56:51 +0000 |
| Subject: | Re: Strict switch statements | ||
| References: | 1 2 3 4 5 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-102266@lists.php.net to get a copy of this message | ||
On Thu, Jun 14, 2018 at 12:45 PM Rowan Collins <rowan.collins@gmail.com>
wrote:
> On 14 June 2018 at 17:16, Alice Wonder <alice@librelamp.com> wrote:
>
> >
> > Should declare(strict_types = 1) do that?
> >
> > I haven't tried, but I would think it should.
>
>
>
> No, it doesn't, and shouldn't. "strict_types" actually means
> "non_coercive_scalar_type_hints"; it's a very specific feature, controlling
> a specific set of situations, not a catch-all "strict mode" for anything
> type-related.
>
> I suppose we could have a new directive that magically changed the default
> for the "strict" parameter of array_search, et al. But if that converted
> switch, would it also convert == itself? And then would we need a new
> syntax for "opting out" and using the loose comparison? Would the resulting
> confusion of people not seeing which mode a file was in be worth it?
>
> This is exactly why I think the word "strict" should be avoided at all
> costs; it's just far too ambiguous.
>
> Regards,
> --
> Rowan Collins
> [IMSoP]
>
Here's an idea, that leaves the flexibility to use strict comparisons on a
per case basis, but doesn't use the === syntax, which I don't like either.
switch($a){
case true:
//will match true, 1, etc...
break;
strict case false:
//will not match anything except boolean FALSE
//other stuff
break;
case false:
//will match 0, null, etc....
break;
}
You could expand this further with a second parameter to the switch, as
someone proposed above, that would put it in "strict" mode, and, allow a
keyword to change things back to non-strict as needed:
switch($a,true){
loose case true:
//will match true, 1, etc...
break;
case false:
//will not match anything except boolean FALSE
//other stuff
break;
loose case false:
//will match 0, null, etc....
break;
}
And, if it's in strict mode, you can still specify the strict keyword, and
if it's in non-strict mode, you can still specify the loose keyword.
Shouldn't even have to make them reserved words either, since they only
have meaning inside a switch statement, when immediately before the word
"case"
--
-- Chase
chasepeeler@gmail.com