Req #79079 [Opn->Dup]: Opt-in strict [and/or custom] comparison for switch/case

From: Date: Wed, 08 Jan 2020 11:20:20 +0000
Subject: Req #79079 [Opn->Dup]: Opt-in strict [and/or custom] comparison for switch/case
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-224773@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=79079&edit=1 ID: 79079 Updated by: cmb@php.net Reported by: phpuser at yopmail dot com Summary: Opt-in strict [and/or custom] comparison for switch/case -Status: Open +Status: Duplicate Type: Feature/Change Request -Package: PHP Language Specification +Package: Scripting Engine problem PHP Version: Next Minor Version -Assigned To: +Assigned To: cmb Block user comment: N Private report: N New Comment: That looks like a duplicate of bug #70440. Previous Comments: ------------------------------------------------------------------------ [2020-01-08 11:09:20] phpuser at yopmail dot com Description: ------------ First, switch/case compares values using *loose* equality (==) which causes issues with null, 0, "" etc. and requires ugly hacks, for instance: https://github.com/doctrine/orm/commit/a9bca86d4d06557b8159b82b2de0b6f678c9dffd I almost always want *strict* equality (===). Idea of syntax (related to the second idea below): switch ( EXPR ) using strict_equality { CASES } (note: that syntax is open to debate, but in any case I would *not* want a declare()). Second (but far less important), sometimes I would like to use a switch/case with a custom comparison (instead of equality). Idea of syntax (related to the first idea above): switch ( EXPR ) using ( BINARY_PREDICATE ) { CASES } Examples: switch (getSomeString()) using ('str_starts_with') { case 'foo_': case 'bar_': /* starts with "foo_" or "bar_" */ break; case 'qux_2_': /* starts with "qux_2_" */ // no break case 'qux_': /* starts with "qux_" */ break; default: /* doesn't start with any of the above */ } switch (getSomeString()) using (fn($value, $case) => (bool) preg_match($case, $value)) { case '/^(0|-?[1-9]\d*)$/D': /* ... */ break; case '/^[[:xdigit:]]{2}$/D': /* ... */ break; // ... } switch (getSomeInt()) using (fn($value, $case) => $value >= $case) { case 50: /* >= 50 (i.e. in range(50, \PHP_INT_MAX)) */ break; case 10: /* in range(10, 49) */ break; case 0: /* in range(0, 9) */ break; default: /* < 0 (i.e. in range(\PHP_INT_MIN, -1)) */ } And as a bonus, it would be even nicer if it could handle specifying built-in comparison operators directly like e.g. "using (>=)" (or maybe "using >="), in which case maybe "using strict_equality" wouldn't be needed. But again, let's not let extra wishes hinder the most wanted: strict switch. Thoughts? ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=79079&edit=1

« previous php.bugs (#224773) next »