Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:110692 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 57965 invoked from network); 21 Jun 2020 15:25:51 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 21 Jun 2020 15:25:51 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 31DB81804F4 for ; Sun, 21 Jun 2020 07:12:32 -0700 (PDT) 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.6 required=5.0 tests=BAYES_00,BODY_8BITS, 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-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-wr1-f48.google.com (mail-wr1-f48.google.com [209.85.221.48]) (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 ; Sun, 21 Jun 2020 07:12:31 -0700 (PDT) Received: by mail-wr1-f48.google.com with SMTP id l10so14103932wrr.10 for ; Sun, 21 Jun 2020 07:12:31 -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=Hhd1biUDEbFhKszmivQxF4AKBgt2mm8JhxV5bV3TV60=; b=XdlFWlLa4AYWc0q4FIeK26X/yteAsSY+cnIE2JxRa17moFHjb+yoPabbKiRbfAOaSy lz2t4I6GWzkZyU7geEBX/CTVY9pCmGhn5tMZO3wQ22/u0ayfMI4lL6+xjAmRBKO7X3px rl/gwG867lcr+cXJ8kW/NmCFlNLMNnjoiSJMG2UvLs3yKN1ago/A/fmdn6ycDMDERSSq ZGLJtSp2BPzXX8vKPQun0qvO0rxE+OY/3CCC59XX1I3mpt83Z8rTjkinzjdIFFJ8QanA YA2lygw3iDcBF2aL4BAuvNiqiwGPE+CpfeM2iHAuBUgdM07xM2drui6ZYLcMsq72gpXs XABg== 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=Hhd1biUDEbFhKszmivQxF4AKBgt2mm8JhxV5bV3TV60=; b=dSlacvnyUdvesr0j855d14DFVWRSDoNH0lWZj+0BSi+OHA8RjSxkaQZr7d0FXR7Ifp EU74xqoA38oxvTsl+IWmsykKS44nGuULTJzRgDazpcwURx6XitkecBz7tXCHACBYMqiH 93dF2lFE7RY+KJ8T6CnS+AAKlcGSffWyE1GVea3JtAZ9AcL+AzBz+FJFCHEDhOR/XOmv A1+azrGHWBFX/k12yhw5Mff72Y/swNlLqOkz2ntFNF1kqDWNoRaESLBMnJ7X/YS4YtJx 1qZ6DrEV3rVnRRXrVoNXc8OX/PZYo9boeiqcD8HbNplSf603GiLveUKEftnc8mz5xr+s R2AQ== X-Gm-Message-State: AOAM531/tbaICMetZ0cEYV/tSSukr9OSyA7rOefxgYVneY4aX4SjlPLM 2g+T8d5EvRB8R3d0OmMZGzMdXhN3 X-Google-Smtp-Source: ABdhPJwO/f4PbeG+Hz6zu4bLdPt1p/TYpM8QihnvMilfrxJNmpt4eF819UyZ6LHPzwy2MfxW40ti0A== X-Received: by 2002:adf:eecf:: with SMTP id a15mr505698wrp.83.1592748747765; Sun, 21 Jun 2020 07:12:27 -0700 (PDT) Received: from [192.168.0.22] (cpc84253-brig22-2-0-cust114.3-3.cable.virginm.net. [81.108.141.115]) by smtp.googlemail.com with ESMTPSA id h29sm14901570wrc.78.2020.06.21.07.12.26 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 21 Jun 2020 07:12:26 -0700 (PDT) To: internals@lists.php.net References: Message-ID: <8dc4cd8c-b84a-e366-8cdd-d791904b677e@gmail.com> Date: Sun, 21 Jun 2020 15:12:26 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.9.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-GB Subject: Re: [PHP-DEV] [RFC] [DISCUSSION] Make constructors and destructors return void From: rowan.collins@gmail.com (Rowan Tommins) Hi Andreas, On 18/06/2020 17:02, Andreas Hennings wrote: > I am ok with restricting the use of "return *;" inside a constructor. > But I don't like allowing the ": void" declaration. The way I look at it, constructors are mostly declared like a normal method - they use the keyword "function"; can be marked public, private, protected, abstract, and final; and can have a parameter list, with types and defaults - so the surprising thing is that there is a special rule *forbidding* them from having a return specifier. If we were designing the language from scratch, would there be any particular reason to *add* that restriction? On 18/06/2020 21:49, Andreas Hennings wrote: > On the other hand, for a constructor the ": void" is just stating the > obvious. > Even if you see a constructor without ": void", you still know that this is > not meant to return anything. > Either because it is generally agreed to be bad (PHP7) or because it is > deprecated (PHP8) or because it is illegal (PHP9) As long as it's possible to return things from constructors, then it is "obvious" that a given constructor is void only in the same way that it is "obvious" that a method called "convertToArray" returns an array. I would be surprised if any style guide would forbid writing "public function convertToArray(): array", so would be equally surprised to see one forbid writing "public function __construct(): void". In both cases, the extra marker takes the reader from 99% certain to 100%. If in future the rules are tightened so that constructors implicitly apply the same restriction as if they were labelled ": void", then it is instead "meaningless" in the same sense that the ": Traversable" is meaningless in this code: class Foo implements IteratorAggregate {     // ...     public function getIterator(): Traversable {         // ...     } } For historical reasons, the IteratorAggregate interface doesn't require the method to declare its return type, but the code using it performs the same check as if it did. Enforcing "voidness" on constructors feels similar - if there was no concern for backwards compatibility, we would probably enforce that getIterator() was marked with ": Traversable" (or an appropriate sub-type), and __construct() was marked ": void", and leave the rest of the logic to the generic code for handling those declarations. Again, I would be surprised if any style guide would forbid writing "getIterator(): Traversable" just because the check is already enforced implicitly by a different mechanism. Regards, -- Rowan Tommins (né Collins) [IMSoP]