Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:107581 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 16083 invoked from network); 20 Oct 2019 02:07:37 -0000 Received: from unknown (HELO php-smtp3.php.net) (208.43.231.12) by pb1.pair.com with SMTP; 20 Oct 2019 02:07:37 -0000 Received: from php-smtp3.php.net (localhost [127.0.0.1]) by php-smtp3.php.net (Postfix) with ESMTP id 3680C2D1AA9 for ; Sat, 19 Oct 2019 16:52:53 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp3.php.net X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HTML_MESSAGE,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS3215 2.6.0.0/16 X-Spam-Virus: No Received: from mail-yb1-xb29.google.com (mail-yb1-xb29.google.com [IPv6:2607:f8b0:4864:20::b29]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp3.php.net (Postfix) with ESMTPS for ; Sat, 19 Oct 2019 16:52:52 -0700 (PDT) Received: by mail-yb1-xb29.google.com with SMTP id t4so2983087ybk.3 for ; Sat, 19 Oct 2019 16:52:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=newclarity-net.20150623.gappssmtp.com; s=20150623; h=from:message-id:mime-version:subject:date:in-reply-to:cc:to :references; bh=y7iRtXg9uQJbcECkTUfEMxsrVSa/0fJEbMpwoKHuhI0=; b=LN5RWS8kuqq+q4y5IjBOWJjJ3+yrFzTK33Nxt5aRTdtk81YAxS1UncuUrJcqwq8gIL G61i0aEUxpdtuWFFbnxbGYp365e6Zr9JVDTCRfh+PcvuTuztyY6M3L9Iu1oGH8Z/1ZMQ 8VxY6zSeKc+dy7/+2x6LwNswznFsAMxj2KhddQNK0mY2kFtCF/tQ6/DgWlyyWhXc0Iml Qt7Ofdm5GZebZWN2+OtF0MAVBaYMunDHhsBgQTBMqF4EU9bPQkKl1GpNZhV6KdmcMhq6 v0itLUjO/qwSPuJsvNf6fU0FmlmcFCigN+1cjALgKClKP1f7DstUQyO9OdvfOCR0ulHf 2yIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:message-id:mime-version:subject:date :in-reply-to:cc:to:references; bh=y7iRtXg9uQJbcECkTUfEMxsrVSa/0fJEbMpwoKHuhI0=; b=H6hQdrgzN9KSNMOi/uUK1MUlOTp2zDjkXLCnwEwZ2fiHP4oRntSERQORfu19wDwTHO YvkHFzVc35zK2O7fvGOJoHroEvueQxpsyQkCTA1yBCHhyCHLmD1SxLbNF10rEW8HI1KL VgTQ6sTsASvA9nKLxZHu46DeWeLNfxB1dLKiUB+Is3a+A+77EJ5yc9xv/vWgSReHlAA4 +LfeWkpBzuaWqpZ7WLdnOhRA7Hw3HBeWZNiZvFEDaoULWufNawkCwRIiS/Bnab2XD5sQ 2nMuhB6Q0Nmki/On8nTRygeBSAOo1KNpV/7aOZ6mXiFg4pj23bztPMn/cTu22W4k91we IOHA== X-Gm-Message-State: APjAAAWra6SV0xKJ2xWFd4rbuD7Htgj7s606tJHqSdeG/dr0RuLcqVke UUoAmd6bMQapn49DfIu2MIWfqg== X-Google-Smtp-Source: APXvYqzgnFLoNT2EHjNOIMXQgIRfRNIwx1yz1gzFqVvly8v+mUrC8pIruIJp5Uz1EaYovmOHo/cwSg== X-Received: by 2002:a25:c6d5:: with SMTP id k204mr11395844ybf.202.1571529172014; Sat, 19 Oct 2019 16:52:52 -0700 (PDT) Received: from ?IPv6:2601:c0:c67f:e34e:4135:43c4:3532:a219? ([2601:c0:c67f:e34e:4135:43c4:3532:a219]) by smtp.gmail.com with ESMTPSA id q124sm2831854ywq.8.2019.10.19.16.52.49 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 19 Oct 2019 16:52:50 -0700 (PDT) Message-ID: Content-Type: multipart/alternative; boundary="Apple-Mail=_738FF947-26CD-465D-A0B9-F497917F7804" Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.11\)) Date: Sat, 19 Oct 2019 19:52:49 -0400 In-Reply-To: Cc: PHP internals To: Kosit Supanyo References: X-Mailer: Apple Mail (2.3445.104.11) X-Envelope-From: Subject: Re: [PHP-DEV] 'switch-expression' and the 'type guard' unary operator demo From: mike@newclarity.net (Mike Schinkel) --Apple-Mail=_738FF947-26CD-465D-A0B9-F497917F7804 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii > On Oct 19, 2019, at 12:40 PM, Kosit Supanyo = wrote: >=20 > Hi Internals >=20 > I've just finished an implementation of 'switch-expression' that have = been > discussed recently. So I would like to present my ideas here. This is great to see that you proactively implemented this. > The basic syntax of switch-expression in this implementation is: >=20 > $result =3D switch ($expr) { > case $cond1 =3D> $result1, > case $cond2 =3D> $result2, > case $cond3 =3D> $result3, > default =3D> $default_result, > }; However, why the need to deviate with the existing syntax, making = refactoring between switch statements and switch expressions more work = that it needs to be. Is there some reason why keeping colons and = semlicolons would not work? =20 Given your syntax manually refactoring existing switches to use = expressions, or refactoring back from expressions to statements will = require more and tedious keystrokes than if you just kept colons and = semicolons. Is there a strong reason to change to using fat arrows and = colons? > You can omit parenthesized expression which is shortcut to `switch = (true)`. > This change applies to switch statement as well. >=20 > $v =3D switch { > case $x >=3D 0 && $x <=3D 100 =3D> 1, > case $x >=3D 100 && $x <=3D 200 =3D> 2, > default =3D> 3, > }; >=20 > switch { > case $x >=3D 0 && $x <=3D 100: > doSomething1(); > break; > case $x >=3D 100 && $x <=3D 200: > doSomething2(); > break; > default: > doNothing(); > break; > } I do very much like this innovation.=20 Would love to see this work for switch statements too. > You can also use `return` and `throw` in result expression. I recalled = some > languages have this feature (but I've forgotten what language). This > feature can be very handy and useful in many use cases. >=20 > $x =3D 'd'; > $v =3D switch ($x) { > case 'a' =3D> 1, > case 'b' =3D> 2, > case 'c' =3D> return true, > default =3D> throw new Exception("'$x' is not supported"), > }; What about break? =20 > Additional feature in the demo patch is the 'type guard' unary = operator > which is an operator that will perform type check on given value and = throw > `TypeError` when type mismatch occurred, otherwise return the value as = is. > It has the same precedence as `new`. >=20 > $a =3D 'This is a string'; > $v =3D $a; // TypeError: Value is expected to be int, string = given >=20 > Just like type hints, can accept nullable types. >=20 > $v =3D switch ($x) { > case 'a' =3D> $result1, > case 'b' =3D> $result2, > }; Nice! -Mike --Apple-Mail=_738FF947-26CD-465D-A0B9-F497917F7804--