Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:100487 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 34152 invoked from network); 9 Sep 2017 11:21:27 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 9 Sep 2017 11:21:27 -0000 Authentication-Results: pb1.pair.com header.from=ilija.tovilo@me.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=ilija.tovilo@me.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain me.com designates 17.110.69.254 as permitted sender) X-PHP-List-Original-Sender: ilija.tovilo@me.com X-Host-Fingerprint: 17.110.69.254 mr11p00im-asmtp003.me.com Received: from [17.110.69.254] ([17.110.69.254:64598] helo=mr11p00im-asmtp003.me.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id A4/F6-10715-5BEC3B95 for ; Sat, 09 Sep 2017 07:21:26 -0400 Received: from process-dkim-sign-daemon.mr11p00im-asmtp003.me.com by mr11p00im-asmtp003.me.com (Oracle Communications Messaging Server 8.0.1.2.20170607 64bit (built Jun 7 2017)) id <0OW000900EKYF100@mr11p00im-asmtp003.me.com> for internals@lists.php.net; Sat, 09 Sep 2017 11:21:16 +0000 (GMT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=me.com; s=04042017; t=1504956076; bh=1ZPz7D4WBLAnk9ohUiY+oAhq5Y2JTTeD4fPlDOZMuZM=; h=Date:From:To:Message-id:Subject:MIME-version:Content-type; b=c7bjSyPIGlv8Ecc1180zKpTOT5ivgqp+T2mcmh2MkYd13q6AWxoIhL1yOjtmBrdp0 lFqiPOyHpPHnfPyQXH52h+oc/dc9Ac9tSz3cviKzTF3y/WbmPiUrvIdQQcC2som3m9 d6Rx5KtzM3oMvYddtJbeA8YMp/gdRIjUqF+HC1+hWYSbpVwgkkYLNgMNuRIurgo0Lf bhzI5BH5ljjkmv1DNYPjVmK1iofkUd0GxlyO0r33LAsTLv/75G4ePP8lD4rfWkaRm9 mr+KA9ZlVQ8sXQI9HlO7sZ5OxCH8V0sDEsDNv4Q9AUTAut2cAEmQO+zQu0p5X4yupn BAYAlfM4uuixg== Received: from icloud.com ([127.0.0.1]) by mr11p00im-asmtp003.me.com (Oracle Communications Messaging Server 8.0.1.2.20170607 64bit (built Jun 7 2017)) with ESMTPSA id <0OW000MGGFJCIA00@mr11p00im-asmtp003.me.com> for internals@lists.php.net; Sat, 09 Sep 2017 11:21:16 +0000 (GMT) X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:,, definitions=2017-09-09_08:,, signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=1 spamscore=1 clxscore=1015 suspectscore=32 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1707230000 definitions=main-1709090178 Date: Sat, 09 Sep 2017 13:21:03 +0200 To: PHP internals Message-ID: X-Readdle-Message-ID: eb28362c-4f8f-45df-bbf0-582e8ad2b8af@Spark MIME-version: 1.0 Content-type: multipart/alternative; boundary=59b3cea5_74b0dc51_17f67 Subject: [RFC] Match expression From: ilija.tovilo@me.com --59b3cea5_74b0dc51_17f67 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Hi everybody=21 Has this idea been discussed before=3F I find myself writing switch statements in PHP quite rarely. This has a f= ew reasons: 1. It doesn=E2=80=99t have a =22strict=5Ftypes=E2=80=9D version 2. It is quite verbose (lots of breaks) 3. It is a statement rather than an expression Often, if / elseif statements turn out to be shorter, safer and easier to= read than the switch statement. What I=E2=80=99d really love is something like a match expression: =60=60=60 =24r =3D match (=24x) =7B =C2=A0 =C2=A0 1: =E2=80=98Tiny=E2=80=99; =C2=A0 =C2=A0 <=3D 10: =E2=80=98Small=E2=80=99; =C2=A0 =C2=A0 <=3D 20: =E2=80=98Medium=E2=80=99; =C2=A0 =C2=A0 <=3D 30: =E2=80=98Large=E2=80=99; =C2=A0 =C2=A0 range(31, 100): =E2=80=98Enormous=E2=80=99; // Equivalent t= o =60in=5Farray(=24x, range(31, 100), true)=60 =C2=A0 =C2=A0 instanceof =46oo, instanceof Bar: =E2=80=98What=3F=E2=80=99= ; =C2=A0 =C2=A0 default: =E2=80=98Unknown=E2=80=99; =7D; =60=60=60 These are the main differences compared to a traditional =60switch=60 sta= tement: - The whole =60match=60 is an expression, the result of the executed =60c= ase=60 is implicitly returned - It allows pattern matching, not just =60=3D=3D=60 comparison, =60=3D=3D= =3D=60 is the default - It does no implicit type conversion, thus no unexpected results when mi= xing strings and numbers - Each case has an implicit =60break=60 =C2=A0 =C2=A0 - Where you=E2=80=99d previously use two =60case=60s withou= t a break, you now combine conditions using a comma One downside, since PHP doesn=E2=80=99t implicitly return the last statem= ent as some languages do there=E2=80=99s no obvious way to do multi-state= ment cases. I assume that some people will not be so happy about having to switch-lik= e constructs. IMO the implicit type conversion is the biggest issue here.= Maybe we could offer a solution for this one at least. Just some random thoughts. Regards --59b3cea5_74b0dc51_17f67--