Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:107620 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 67738 invoked from network); 22 Oct 2019 00:16:34 -0000 Received: from unknown (HELO php-smtp3.php.net) (208.43.231.12) by pb1.pair.com with SMTP; 22 Oct 2019 00:16:34 -0000 Received: from php-smtp3.php.net (localhost [127.0.0.1]) by php-smtp3.php.net (Postfix) with ESMTP id 115FC2D2001 for ; Mon, 21 Oct 2019 15:02:19 -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=-0.5 required=5.0 tests=BAYES_00,BODY_8BITS, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: X-Spam-Virus: No Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) (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 15:02:18 -0700 (PDT) Received: by mail-wr1-x42b.google.com with SMTP id p4so15673907wrm.8 for ; Mon, 21 Oct 2019 15:02:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=EDPgBfQHk9hrS2brbelfbN0WzVKouc7tC/4qdeuPxUU=; b=POyQiurpWI+9Z6UnhKSZEtoZE+cc5aczCQPERSJ6wF7wNEob/zI2NQWVBwvBKbuk0E aRyPWkgybZ9EFRQUDA9mvJH7Uh5QnA8bK4RDq4jt57EEK7adAAPoT8hu0NsY/GwNWMLm YtSn0ZVQZBBFEM6s5ZE6yHt5f2LfcjZmHDYcG7jd6ewMNQpgnWxDlhDmAb+IIRw+Ph+e fDGLxQFMLdvG8RqjnVEpqKPe5QqJtbL++VPWjChg4S7qHdeMZQyAvYqMRVJvhEZqYOFs LwzTDWfUtDWTa8qyQGNx3QkmwlQzHyBA3LyKAgG+gJKkK0VEMsscBtNR+b8nAD7Qid/J r2zA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=EDPgBfQHk9hrS2brbelfbN0WzVKouc7tC/4qdeuPxUU=; b=gBIKLvgeKWn5Qk+p57VnQTmShS/Z5Kf64tRMzA8LOGHbPitBql3l1sGIL2had1/UkV 8FS1Le0IrFmBnXf6YCkRnRyuuPVMgdoX6QCt1UKw103C54Y2q9SuUtMR2p2auwT3qHcZ +S2G8tZF4j2tNAFzMo1+vqf/lj0AZGEDCv/XgDEsbTVC84lmvfvzWSVK4LyBxX6JPDIP fMwSKmh35mp/o4JUBDOEkgCL8iSnX+4SHdWV8PPokNY5TLUttw91BQ42l0M1qJNGvXb9 pD3k/8HWsF3vqCGo5qIQxUGWBK86jjkogTOUa58tLdNywdTGhWlNt58g+gG8MzosVaqG H88w== X-Gm-Message-State: APjAAAVWHLDAtdmUVWD1F9BGqrKlDsyjvnKZXPhvt5vqJ90GeugLruix FxouX0TcXgf5hedNLpxzyyYDS+9YWyU= X-Google-Smtp-Source: APXvYqw7wKaKB0FiMcboR7mNQqpaoNvxIW09cAOgb/LJzKf7/siJgDwoeiQqDLtKNtYm1YbfB5ZIew== X-Received: by 2002:a5d:498e:: with SMTP id r14mr297513wrq.36.1571695337202; Mon, 21 Oct 2019 15:02:17 -0700 (PDT) Received: from [192.168.0.16] (cpc84253-brig22-2-0-cust114.3-3.cable.virginm.net. [81.108.141.115]) by smtp.googlemail.com with ESMTPSA id 79sm24692029wmb.7.2019.10.21.15.02.16 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 21 Oct 2019 15:02:16 -0700 (PDT) To: PHP internals References: <3D85D31D-5DDD-42AA-B465-6ADBE72D952A@newclarity.net> <9A432898-EB7A-45C9-9C6F-AE6392B9B5C3@newclarity.net> <10B38D5F-C2BF-4657-BC58-B302C096A18F@newclarity.net> Message-ID: <5f2f13a9-7500-9c5c-7a2a-dfc883b2907b@gmail.com> Date: Mon, 21 Oct 2019 23:02:14 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.9.0 MIME-Version: 1.0 In-Reply-To: <10B38D5F-C2BF-4657-BC58-B302C096A18F@newclarity.net> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-GB X-Envelope-From: Subject: Re: [PHP-DEV] 'switch-expression' and the 'type guard' unary operator demo From: rowan.collins@gmail.com (Rowan Tommins) On 21/10/2019 21:18, Mike Schinkel wrote: > > But to follow up to clarify what I was thinking consider the > following/ (hypothetical) /PHP syntax. Note that the return value > has two comma separated values, and the assignment can accept > those multiple values into two comma separated variables in the > assignment: > > > function read_file(string $filepath):string,PHP\Error { > $content = file_get_contents( $filepath ); $error = false === > $content ? new PHP\Error( "failed to open '%s'", $filepath ) : > null; return $content, $error; }$content, $err= read_file( > __DIR__ . '/myfile.txt' ); if (!is_null( $err ) ) { > error_log($err->message); } > Ah, I see. Yes, multiple return values would be nice sometimes. Note that you can get a lot closer to this in current versions of PHP than your example, because you can use the [...] syntax for both creating and destructuring arrays:         function read_file(string $filepath):array {            $content = file_get_contents( $filepath );            $error = false === $content               ? new PHP\Error( "failed to open '%s'", $filepath )               : null;            return [ $content, $error ];         }         [ $content, $err ] = read_file( __DIR__ . '/myfile.txt' );         if (!is_null( $err ) ) {            error_log($err->message);         } > With the first example PHP could type check at load time vs. > having to wait for coverage of this code at runtime, and `:array` > type hint in the latter is not as good as the `:string, PHP\Error` > type hint of the former. > The ability to typehint multiple return values like that would be indeed be useful, but I'm not sure it would need to reserve that syntax. One way it could be achieved is using a "tuple" declaration, like in Hack: https://docs.hhvm.com/hack/built-in-types/tuples In general, grouping things with commas and no parentheses is always going to *look* ambiguous IMO, even if you can limit where it's used to not be *technically* ambiguous. Regards, -- Rowan Tommins (né Collins) [IMSoP]