Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:107580 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 43158 invoked from network); 19 Oct 2019 18:55:58 -0000 Received: from unknown (HELO php-smtp3.php.net) (208.43.231.12) by pb1.pair.com with SMTP; 19 Oct 2019 18:55:58 -0000 Received: from php-smtp3.php.net (localhost [127.0.0.1]) by php-smtp3.php.net (Postfix) with ESMTP id 2766C2D204C for ; Sat, 19 Oct 2019 09:41:09 -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=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,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-oi1-x22e.google.com (mail-oi1-x22e.google.com [IPv6:2607:f8b0:4864:20::22e]) (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 09:41:08 -0700 (PDT) Received: by mail-oi1-x22e.google.com with SMTP id 83so7796487oii.1 for ; Sat, 19 Oct 2019 09:41:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=chRpKjPNaHNNpobUVd+61sYFntwf2/XhBDhu3G/GfOw=; b=OxbIxo44alLSOA2NoDfTsir4P4ubvZ5EjN/EUNBAflIUBG8ScpHwceXa4RwuqYKHtS Y2SwTDBcpAQwZ/8PlUJjhkDMorzETtXHys5D1nEo+eG36jxaZ0o01Exud8BrHaXW1pft 67+m/Jdk1KwWQlMvFOi3bQ0guSfrkwJ/UTV4zqpysLpqDiiYx8fp3CxplQaIXU/ZpkCb 2q8aJAFDP8nspcIKh/G9Xfu7Mho3d7YaD0OQIjBXqD+lmCYD1nt6hhyL4TC/Hg86j1in ZPSmlk9V4kF1UuPXwXnKMdQYu6S1xnpnWCAdBH+FbJQyMYGCaXBmThxEwfolLL1A6VqM 4zqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=chRpKjPNaHNNpobUVd+61sYFntwf2/XhBDhu3G/GfOw=; b=rBXnKIts0DFl28nxpcJ14aGmMQlwFJWpvwwvw00zEVSI9gfrRSQu1H7djehPXTlxMS dAZ2ON7eClGj1eT8Y/RWQwp97D0zernROBcyVQnscopl/U95/qPfzEsDKBMKmFlB2R/c /E89Kb+eu6ZgeNreKwOBWqQdkBZWShMoulyJL0fKm4YEkMhFP8Q1WtALTXwV+wVKA73l QP6ivt+QZX/UZQIaRW4vMgjZovYhKl9Z5FVKezP7ehrIbPb4MA59GEaJz+davbO1m6x2 S+x6z4Em5Jze9pMMrIwyD7O3YHNwCGhaAbrOiZbmBLByuNepIR5N0E6q4GHLZzA0MOj0 hQ7A== X-Gm-Message-State: APjAAAXhAiVPaWBqBn2DJYLuqDQTyHGtX9JIOBzOdecEqdbeGL7mxL1Q V3oaoAEustKEJ2iAHVIJt43CA4U4SFBHBq8u4COFDQwu X-Google-Smtp-Source: APXvYqw0MgIEWsIv3HDNtvTASHXa7mGtQ4x3ma73J1euEm+p10WOKw8v4w2e27fIzAtXwc5m5AukbbtHKvUEvh+nbns= X-Received: by 2002:aca:c3c6:: with SMTP id t189mr12124451oif.15.1571503267846; Sat, 19 Oct 2019 09:41:07 -0700 (PDT) MIME-Version: 1.0 Date: Sat, 19 Oct 2019 23:40:56 +0700 Message-ID: To: PHP internals Content-Type: multipart/alternative; boundary="0000000000000b1d960595461e9e" X-Envelope-From: Subject: [PHP-DEV] 'switch-expression' and the 'type guard' unary operator demo From: webdevxp.com@gmail.com (Kosit Supanyo) --0000000000000b1d960595461e9e Content-Type: text/plain; charset="UTF-8" Hi Internals I've just finished an implementation of 'switch-expression' that have been discussed recently. So I would like to present my ideas here. The basic syntax of switch-expression in this implementation is: $result = switch ($expr) { case $cond1 => $result1, case $cond2 => $result2, case $cond3 => $result3, default => $default_result, }; Like function declaration and function expression in JavaScript, if `switch` appears as first token at statement level it will be recognized as statement but if `switch` is in expression context it will be switch-expression. switch ($expr) { case $cond1 => $result1, case $cond2 => $result2, case $cond3 => $result3, default => $default_result, }; // Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW) But this is OK. !switch ($expr) { case $cond1 => $result1, case $cond2 => $result2, case $cond3 => $result3, default => $default_result, }; // semicolon is still required because it is an expression You can also use comma to associate multiple conditions with single result expression. $result = switch ($expr) { case $cond1 => $result1, case $cond2 => $result2, case $cond3, $cond4, $cond5 => $result3, default => $default_result, }; Which is equivalent to: $result = switch ($expr) { case $cond1 => $result1, case $cond2 => $result2, case $cond3 => $result3, case $cond4 => $result3, case $cond5 => $result3, default => $default_result, }; If there's no default value or switch-expression will just give NULL. $x = 'c'; $v = switch ($x) { case 'a' => 1, case 'b' => 2, }; var_dump($v); // NULL Empty switch-expression is also allowed. $v = switch ($x) { }; var_dump($v); // NULL You can omit parenthesized expression which is shortcut to `switch (true)`. This change applies to switch statement as well. $v = switch { case $x >= 0 && $x <= 100 => 1, case $x >= 100 && $x <= 200 => 2, default => 3, }; switch { case $x >= 0 && $x <= 100: doSomething1(); break; case $x >= 100 && $x <= 200: doSomething2(); break; default: doNothing(); break; } 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. $x = 'd'; $v = switch ($x) { case 'a' => 1, case 'b' => 2, case 'c' => return true, default => throw new Exception("'$x' is not supported"), }; 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`. $a = 'This is a string'; $v = $a; // TypeError: Value is expected to be int, string given Just like type hints, can accept nullable types. $v = switch ($x) { case 'a' => $result1, case 'b' => $result2, }; Also works with variable references. $x = &$a; $y = &$functions[$name]; $arr = [&$obj]; This operator can also be used to simulate typed variables. The demo is here: https://github.com/webdevxp/php-src P.S. This is just a bunch of ideas with concrete demo, not intended to be pull request nor proposal because I think I'm still new here and inexperienced in many other ways. Cheers --0000000000000b1d960595461e9e--