Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:110588 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 29067 invoked from network); 16 Jun 2020 14:14:12 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 16 Jun 2020 14:14:12 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id E21791804FD for ; Tue, 16 Jun 2020 05:59:38 -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=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE 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-ua1-f49.google.com (mail-ua1-f49.google.com [209.85.222.49]) (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 ; Tue, 16 Jun 2020 05:59:38 -0700 (PDT) Received: by mail-ua1-f49.google.com with SMTP id a10so6860924uan.8 for ; Tue, 16 Jun 2020 05:59:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=basereality-com.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=aRpX5mRyBv3xKHLLLHCLVAo4Nzgemn81PXOtgTNtH2Q=; b=cuf4pyLO1yXfwgWIoZ/bYs5UHIipHkYSlVjiAELnk0h3CXfQ8kTwUb1+mW7shNTuR4 Jm6IZt0bh69KgtAZicDADuO0zDCHeeVJUaZezJvCz4HfCdTKlDYFT38XbxzYAtTRAxCj QjizcfF9jawqJEc8/AYoU3kbXA+sl7Qg8RDBlXiu/YkfUGE6Ur3c2rt3s5WEH8QIleKV ou1Qtl9P8NhXz1nk9NVAQ0PinfhVFjRT5OhD7hIxg44gQEafF7XQft3tluwR7JRsY6fJ 30RanwESngBnwe1eLsUqJMYw1yqqnRFfg41bEtfrtauYmjuqpvTAgZu6RkPPnLCcBtSP cgPg== 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:cc; bh=aRpX5mRyBv3xKHLLLHCLVAo4Nzgemn81PXOtgTNtH2Q=; b=XxsKsVPsQklCDHAnvIOKlWbczTUrpGCvdnRDURIMatlevoU6BntwanuWjz/Au+bOKY Hows0Lqubxkt6CPeea+ILrR6kh+x0xIBufkpM0joajbflPreK1JTG5pLctAFQmacS1w7 gsr5O/iN+jIGJO19tC3l6OaLG0UQA/5bN0XihPmltj1soRHLyMUuTKYsMII1Z5+85Led /iJII4iH4QIu0QwzriWRUg4u+Sd2rc/Utk1xJdbxjKs8tz9s8XcwL6tjno5W8os8bD5s SFyiILKNrkTqsy7bYxYHB97HaN3Bvx9DLurmi+u1XQSXUM6OvVKRFKYUMXjLt26XzRnk jtSA== X-Gm-Message-State: AOAM533jxRh+Y4MhS1cno+ZfbaxhP3Or0VoLSeeQwGxMHYDKtHYSWopG STIHX3GAJEUaf93nGnsy5hCNYtp+xTJBdmwDVxfgCA== X-Google-Smtp-Source: ABdhPJwguc81BuckdEA/HiXSAviw++2Gt9o09BrvhuL+DLx5TDxAGkgwcGAoWI/GpMHk4x70bAwOQJqAnc1CrY4KMPQ= X-Received: by 2002:a9f:3611:: with SMTP id r17mr1714231uad.108.1592312377381; Tue, 16 Jun 2020 05:59:37 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 16 Jun 2020 13:59:26 +0100 Message-ID: To: Benas IML Cc: =?UTF-8?B?TcOhdMOpIEtvY3Npcw==?= , "G. P. B." , PHP Internals List Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] [RFC] [DISCUSSION] Allow void return type for constructors/destructors From: Danack@basereality.com (Dan Ackroyd) G.P.B. wrote: > Seems like a no brainer and a good addition. :) For situations where stuff is simple, 'no brainers' are okay. For situations where everything is horribly convoluted, 'no brainers' are often bad. Benas IML wrote: > even though no return type means mixed|void Not quite. No return type is _treated as_ 'mixed|void' in some circumstances particularly signature checks during inheritance. That's not quite the same as 'meaning' the same. > What I meant, was that since no return type means `mixed|void`, > you can't do the following: I'm actually not sure exactly what you're trying to say there, but I think pasting the text I reference whenever trying to think about LSP might be useful... "PHP allows contravariance (aka type widening) for parameter types to obey the LSP principle. A subclass may use a 'wider' aka less specific type in place of the inherited type for a parameter." "PHP allows covariance (aka type narrowing) for return types to obey the LSP principle. A subclass may use a 'narrower' aka more specific type in place of the inherited type for a function return." > I am proposing to allow void return type for constructors/destructors. Constructors in PHP have multiple issues that are 'surprising'. I'm not sure that fixing only a small part of how they are surprising, improves PHP drastically. Also...I really wish we already had null as a usable type, as most of the uses of void are slightly inaccurate. The meaning of void and null are: void - this function doesn't return a value null - this function returns a value that has no information in it. Presumably you want to be able to specify void as the return type to make it be easier to detect errors in code like the example you provided. It seems that making a rule for whichever static analyzer you're using, that errors on any assigning of a value from a __construct call, would achieve that, without touching a part of PHP that is full of edge cases. To be clear, I'm not sure if I am in favour or against the RFC. The real problem is that constructors just don't fit into how we think about normal functions is PHP, because of the magic that goes on around them and so both void and null as return types would be a little bit incorrect. cheers Dan Ack Some example functions using those two return types. function this_is_void_return(): void { exit(0); } function this_is_also_void_return(): void { throw new Exception("no usuable return value"); } function this_is_also_also_void_return(): void { while (1) { echo "Hello world\n"; } } function this_is_null_return(): null { // deliberately empty. } // This code is fine. var_dump(this_is_null_return()); // This code is never going to execute the var_dump, which // might indicate an error. var_dump(this_is_void_return()); var_dump(this_is_also_void_return()); var_dump(this_is_also_also_void_return());