Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:107592 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 81525 invoked from network); 21 Oct 2019 11:25:20 -0000 Received: from unknown (HELO php-smtp3.php.net) (208.43.231.12) by 76.75.200.58 with SMTP; 21 Oct 2019 11:25:20 -0000 Received: from php-smtp3.php.net (localhost [127.0.0.1]) by php-smtp3.php.net (Postfix) with ESMTP id BE5222CA32B for ; Mon, 21 Oct 2019 02:08:58 -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-io1-xd29.google.com (mail-io1-xd29.google.com [IPv6:2607:f8b0:4864:20::d29]) (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 ; Mon, 21 Oct 2019 02:08:58 -0700 (PDT) Received: by mail-io1-xd29.google.com with SMTP id q1so15032629ion.1 for ; Mon, 21 Oct 2019 02:08:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=56khhjX7RJ0fk4UMdkY8TMwMncOJi/G7E+Anrs4EZmE=; b=fneLBpM/TGji7qDB4b0D0+1TVJBWjvXhI87czSw6npHbR6KmdWGNmjiLJ5mrZpC/4u C2ry7r/O3Hl0o5iuHP6DcG+LDDqu1eb005fEmWAfWRJnvUkvsYavgnZd2eWF88+WAC/S M9nUH0dMB77ZlSrGUEM/c2g+YN3Bue8Zey2KYEvU/NZjsxpWFXVfxdmjj5IkrsyEo7gH xSB3+TyYIAd3DMNMZn0bfOR8bi6ORPTM5XpxeHf5yy3zXL5NGNE8TejUwXrdA9RvdPRs jWmwT/WnpI+jkdJ1XKjUvdUuuZeTmegui6K5eNSkZGxX5zpUkGur/gMS2FmlMLzSPHEm KDkw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=56khhjX7RJ0fk4UMdkY8TMwMncOJi/G7E+Anrs4EZmE=; b=WjbJStBgeDN9EWVUg0vaIhZaHy17CYEBwAg43YamG+H26sZaqUI0lR63sBhMUuY/qW pJ0RZLkY+NKpjGFqev1X2nVlEhZknu4rH/ySJo/RJ8+KMmZrENlzJ5PZbdxeGIpd7dd3 UgBVitecwyhtUVCU5CrTkm6pw+pmxdw8+LqA2LS6yyKSxo4T4QDH82BydPgfLZUAluyM WrBUfb3s1I/aiCPFGapshoev6qrN3hyp9tuE2kmwKGVmReIwYAw9Hmlar2clGfTG07DR Jvm/9TD2XKOIj1kdExjh2mprI58LcHxdjQPam30fXyH/xdtN+DBytgzVT40wPKfdWtUS 0wKA== X-Gm-Message-State: APjAAAUepJcqoA9ikxq8GlTI74xiRHUwkvzHXLt5EKXdkjN2Y5d6qg+3 Y8galhFQy2derhFgYgJwQ5OWcTW9Ky9Rrpjuv6bRP+dH X-Google-Smtp-Source: APXvYqxEIdU1WByMLK9a3SuYsR1h/YVwM/j+UZFjF3EwNQihB3XjJ3bmY2VxhybxNmEzst16hISWARFwzhf9YGUqWQo= X-Received: by 2002:a02:82:: with SMTP id 124mr21615411jaa.131.1571648937260; Mon, 21 Oct 2019 02:08:57 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Mon, 21 Oct 2019 10:08:46 +0100 Message-ID: To: PHP internals Content-Type: multipart/alternative; boundary="0000000000009dfbca0595680853" X-Envelope-From: Subject: Re: [PHP-DEV] 'switch-expression' and the 'type guard' unary operator demo From: rowan.collins@gmail.com (Rowan Tommins) --0000000000009dfbca0595680853 Content-Type: text/plain; charset="UTF-8" Hi Kosit, On Sun, 20 Oct 2019 at 16:20, Kosit Supanyo wrote: > You can recognize the difference of those by looking for `=>` right? But > the parser generator (bison) cannot do that in LR mode (it maybe can in GLR > mode but I'm sure that would be unacceptable due to performance losses/more > memory usage). So workarounds have to be used. I did it by simply return > different tokens from the lexer base on previous token before `switch`. >> >> Thanks for clarifying, I thought it might be something like that. > * Different comparisons applied to the same variable/expression, e.g. >> match($user->getScore()) { case <0 => foo(), case >100 => bar(), default >> => baz() } > > > I'd thought about that feature too. But since I also introduced type guard > operator which uses `<` token it would have parser conflicts and simple > workarounds cannot apply. > I'm not sure is the right syntax for type guards, but it's possible we'd want a different syntax for new switch/match functionality anyway. For instance, using a placeholder for the value being tested, like $$ (a common choice in other languages is _, but that's a valid and commonly used function name): match ( $user->getScore() ) { $$ < 0, $$ > 100 => foo(), $$ < 10 => bar(), default => baz() } You could also allow the placeholder in the opening clause to define the same operation for each case: match ( $user->getScore() < $$ ) { 0, default => foo(), 10 => bar(), 100 => baz() } Regards, -- Rowan Tommins [IMSoP] --0000000000009dfbca0595680853--