Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113469 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 67880 invoked from network); 11 Mar 2021 09:59:02 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 11 Mar 2021 09:59:02 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 463D31804D1 for ; Thu, 11 Mar 2021 01:51:31 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.2 required=5.0 tests=BAYES_20,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-wr1-f42.google.com (mail-wr1-f42.google.com [209.85.221.42]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 11 Mar 2021 01:51:30 -0800 (PST) Received: by mail-wr1-f42.google.com with SMTP id e10so1072822wro.12 for ; Thu, 11 Mar 2021 01:51:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:references:in-reply-to:subject:date:message-id:mime-version :content-transfer-encoding:content-language:thread-index; bh=NnlJe+D/lNSnn1iINqBuhdjMUEpsteUJxi3JjS5d8Ug=; b=oQ4Bc/RS1HoFstnvUu8vKp0lBxXA7FAmWgNnKywlkZ7Wxusiv6YpKyEDiGBHAii+FA 1wkTtpGqUBhBeXN2TU8zHyysUZammwpqzba4j8BhkocAfY7AaOEW3ZkuaRXBDht1N0YI SJhDV7QkF+2evywhJ1ZlIJ6cgoBHezTd4ZtG/i3KsKVJ0Y8bApEjlGFS0OMaYLZSTluW HiBsm+ek7hQKqDTVerTcnmUUunEds+xxPY0gmzEhuauWav87x6sh/VbBeyzMXLZGY0I4 BFmt4A8wCxhHUYBxNa/+BVgDnfF91mZtkAjj5TuEg8+HoqxDZ0GdUMVd+Ssq87qBDnAM yshw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:references:in-reply-to:subject:date :message-id:mime-version:content-transfer-encoding:content-language :thread-index; bh=NnlJe+D/lNSnn1iINqBuhdjMUEpsteUJxi3JjS5d8Ug=; b=WA3wLSVUAEbNgEbL2EguQK1hxZPGbXyUiBrOIB4HLSSvsT/2SufVIvd5w4KDuoI7g7 AIXKYX8lwJlvQlV8yBbSvKZ5QwDw47I+Lsm6tQb2UhidfaxmBzlj8+xDGTWWvpsTGveV TwehowejFfhHcCPxr2JEXogv/TLV9lK75CsO/DOXtrFIMotzD711LJyl8E9x1Sj70yPE +3rE0uZHLJxIXIp2+BzuU3SjTP8rV0QAwz7wFthBk2iieQrD4V9yHbCpI3ZAn7sHiEUC mU1vx3wuOYlsxQOw1yzam1u3jrpPtd9nvi50y/dqzx5AUEbC0dtgFNPkO2g+MZVzJaLQ gQvA== X-Gm-Message-State: AOAM531Bsk5w/zj/0iTaqUKJkFJ2feGIb9ABF1BpW9PWcCcCDTcbkqdv K6mM+WJP27HL/LIRVgReeecf3Hk/NfmENQ== X-Google-Smtp-Source: ABdhPJy8Or4CR+JGnYZNA+vzoHOE05rgwSZLZrx68ZdmqqorgnhIGNQbTUjUROG71AdvL2X/n9dGiw== X-Received: by 2002:adf:c587:: with SMTP id m7mr7580097wrg.369.1615456286228; Thu, 11 Mar 2021 01:51:26 -0800 (PST) Received: from DESKTOPQD1QNOQ (static-12-12-248-43.dnt.net.pk. [43.248.12.12]) by smtp.gmail.com with ESMTPSA id a75sm2886251wme.10.2021.03.11.01.51.23 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 11 Mar 2021 01:51:24 -0800 (PST) To: "'Rowan Tommins'" , References: <01fa01d71627$edb078a0$c91169e0$@gmail.com> <0DC08797-11DD-4D45-8C43-5678822DA259@gmail.com> In-Reply-To: <0DC08797-11DD-4D45-8C43-5678822DA259@gmail.com> Date: Thu, 11 Mar 2021 14:51:21 +0500 Message-ID: <000f01d7165c$1923ae10$4b6b0a30$@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Mailer: Microsoft Outlook 16.0 Content-Language: en-us Thread-Index: AQEkxYUdW5hh6aEiWom8mpkeVucJUANI72gQq8jla7A= Subject: RE: [PHP-DEV] Honour the Default Value of 'return' Statement According to the Function Signature From: office.hamzaahmad@gmail.com ("Hamza Ahmad") Hi Rowan, Thanks for the response. Luckily, you razed the question that I thought = about after posting the first request. My idea develops on respecting both return type hints and union types. = The current implementation of empty return does not respect the function = signature. As I have mentioned in the first message that empty return = returns null regardless of the return type set, I propose that an empty = return may return the defaults of types i.e. an empty string if the type = is string, an empty array if the type is array, zero if the type is int = etcetera. I have also noted that in a void typed function, an empty = return may return nothing and just end the execution. The current implementation of empty return is only favourable for mixed = type. In other words, when the return type is mixed, an empty return may = return null. Otherwise, it should respect the declared return type. After the release of union types, there emerges a question regarding the = selection of default return value. To solve this problem, the type on = the very right side will be used. Now, I come to your question, Rowan. As of today, only false and null = literals are supported. If both assist in error handling, why does empty = return always returns null? I tested this code on shell. var_dump($a=3D(function():string|false{return;})()); It produced: Fatal error: A function with return type must return a value in php = shell code on line 1. Here comes the inconsistency. To resolve the problem, I suggest to = respect the literals first. If literals are not found, respect the first = type on the right side. Moreover, only two standalone literals, false and null, will be = supported. Otherwise, the literal should be one of the types specified. = If multiple literals are specified, it will throw an error of multiple = literals. The literal can be a simple literal, such as, "unknown" 0, and = a constant expression, like self :: MSG_UNKNOWN. It will help maintain = the multi-language scripts. I hope I have been able to make my stance clear. Best Hamza Ahmad -----Original Message----- From: Rowan Tommins =20 Sent: Thursday, March 11, 2021 1:45 PM To: internals@lists.php.net Subject: Re: [PHP-DEV] Honour the Default Value of 'return' Statement = According to the Function Signature On 11 March 2021 03:37:52 GMT, office.hamzaahmad@gmail.com wrote: >function get_nationality_string(int $country_code) : string | "unknown" >{ > return; >}; If I understand you correctly, your idea is that the "return;" here = would be treated automatically as "return 'unknown';" I think that's an = interesting idea, although I can't immediately think of a situation = where I'd use it. My main concerns are with the syntax: - Firstly, specific literals aren't currently allowed in return types, = and allowing them would have other implications. Three exception is = "false", which is allowed because of its common use as an error code, = including in many internal functions. - Secondly, it could be ambiguous which is intended to be the default = value, if the return type was something like int|"yes"|"no" Perhaps the default return value would need to be specified separately = from the return type somehow? Regards, Hi Hamza, Welcome to the list, and thanks for sharing this idea. -- Rowan Tommins [IMSoP] -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, = visit: https://www.php.net/unsub.php