Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:125262 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id 285A91A00BD for ; Mon, 26 Aug 2024 10:32:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1724668476; bh=ZMBxCnhu+AY/YZImUqLEa+eBHVIJFM1CMQhPyW1U3XI=; h=Date:Subject:To:References:From:In-Reply-To:From; b=lU39TqSoIerHJPTmbHub3hPhbNcneuYuIl/T9yk12dLub0JE+JFdCvYvXYuX5mFkA uDN/usq6BzE0hHgBNYOlZgmPy7qofwG9ybdCE3tSN2GtoEW9GwVO4T6JCwegm+3FXB VZN+SsorfeVGn8aHvNDIKzIJTRnxx0QZde+0c1aaubt0YG3Rt4qBO1OxqnmGNme5ln 7d0ykJenJdnPgt6dAopvp0oOp+6zaEqjyNmoBWaiAFuE0NXanCSu0SW/5fPcKFUPNk KN5d5678LOR8rx+Z1jZLNKB++IdFCUnCLzkMgrOgFHwxNOPrnvY1KwDSLRQ0oM7y25 Nq8FL7GoEso6A== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id AF44E180034 for ; Mon, 26 Aug 2024 10:34:34 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=0.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail.stella-maris.solutions (mail.stella-maris.solutions [46.101.232.159]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Mon, 26 Aug 2024 10:34:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=heigl.org; s=mail; t=1724668360; bh=ZMBxCnhu+AY/YZImUqLEa+eBHVIJFM1CMQhPyW1U3XI=; h=Date:Subject:To:References:From:In-Reply-To:From; b=Ol0rXhqIB7JhOXjY1H18qCofi2RTcVff+9fPv3/ZnwSYSnsesNWAq1uZElcwydKX0 CG3WqN3lXCnYsE6wo8LQSkBEIruCe8tWOlRCg6j8Gk6QkhyNQ7r5C1z1M20V9G/Ozy /VCJVNYBN6NMUoMixhCnm2frULhSb6jqKnrWmAsT3+AcVmdOQr7iJYFQA/ljJKatl9 FqA6WF1MpM04efPecF4JRndWwt3q+GFhmZYnN+NPevlMtqiRqInTFEhVvkFNR7xDe/ oDxN9wmaJMflgGC1BMSz/ExMpGUVSgXbpiZCUt44tdbuqnqn1Meitw5x8MGuWJLtBX u0k+5p+e7wLWw== Received: from [192.168.1.119] (heigl.gw.tgnet.de [80.72.250.242]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mail.stella-maris.solutions (Postfix) with ESMTPSA id 0F2E97E001 for ; Mon, 26 Aug 2024 10:32:40 +0000 (UTC) Message-ID: <5c980153-228a-4544-8fb7-09a1c265ef14@heigl.org> Date: Mon, 26 Aug 2024 12:32:39 +0200 Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PHP-DEV] [RFC] Default expression To: internals@lists.php.net References: <0c8ed5d6-5507-4c41-8d7f-05d14ba8aa4c@scriptfusion.com> <15301ee5-7964-4f0b-8804-a19f95f43152@gmx.net> Autocrypt: addr=andreas@heigl.org; keydata= xsFNBFzEA7MBEACpvo0AbmZG6lUGMvDUebQcYV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G5iyl52NgM2PH pm98XP8AEQEAAcLBdgQYAQoAIBYhBFnu0GaGtc0AdpRwDajVQ37Ock/lBQJcxAW4AhsMAAoJ EKjVQ37Ock/ly/cP/1SSqtqsRsUtSBkmmqMHJ3okcfPGXwU85LWpIW6l4Z1hcD/b7k2h3Es0 aMBSFc3mueZP+ZkHirk0xLe5NGG9vZcKE/oqotdlf+9xASGlCviU+WT9vG50F5JKqqUc3K6O JQ84uYHmpreyjLFg5mKgh8l1NsV3gC56ny79Y37CSTHqA8y5fKpedKptc/rveFTl3/eHZapl fb+kFqMcMNpNsTVgQHlA9mM1FwGZ3KV2witTCERQR5m4OBwGqZqRVHsD37DuD6VBCahtSAOq 5fXLGZ3PewbwgoWukmTL1xqxCIv2ozXw9JlNqdyMuRdVFzcfciZTM4kL6lM4gOzElqJU9mWK /7p0q3JF1Ie5QMBOLavEYYV2dnIy/ubm5P8RhQScnJ/mCqE+YJmfoMBq5bjdFSRaIH6WNkSW xfI/FW1EfrxisZbrIMngu7hBQKFbCFCRUN0Gj7RppyGYD9yks+x7GZAMIlxyeT+sKR9wljLu CWjkJTjKNDT8UWTr7XwJHQ2/ouI3fmI77iZCZbq9aesobv9NnBO/waAzjYAEx2ahZmM7m1rQ lPS636tUZ1QVZx/4SzNiMTBsgWLDQ0VtxcEmYe54+r8Y4o0vSrMcDYgBczZe/ASpSJB4AI1l Q3o3s5p3AeYsYPBtBsysSAbd0uZ65EDc9xwSRLiMJj+x8jGy2xtu In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit From: andreas@heigl.org (Andreas Heigl) Hey folks. Am 26.08.24 um 11:26 schrieb Bilge: > On 26/08/2024 10:03, Andreas Leathley wrote: >> On 24.08.24 18:49, Bilge wrote: >> >> For me there is another question. When using interfaces and classes, >> default values can be introduced, like this: >> >> interface CompressionInterface >> { >>     public function compress(string $data, int $level): string; >> } >> >> class GzipCompression implements CompressionInterface >> { >>     public function compress(string $data, int $level = 4): string >>     { >>         // do something >>     } >> } >> >> When I have the GzipCompression class, I would know there is a default >> value for $level, but when using the interface there might or might not >> be a default value, depending on the implementation. As far as I read >> the RFC, using "default" when there is no default would lead to a >> runtime exception, but there is no way of finding out if there is a >> default if you do not already know. Being able to test that could be >> useful, although I am not sure about the syntax for that. In the example >> when getting CompressionInterface, I might test for the existence of a >> default value of $level and leave it at the default if there is a >> default (maybe I know that some implementations have a default value, >> others don't). One could test the specific implementation with >> instanceof checks, but the advantage of "default" could be that you do >> not need to know the implementation and could only adapt to possibly >> defined default values. > > Hi Andreas, > > Thanks for this question; I find this super interesting because it's > something we haven't thought about yet. I must admit I completely > overlooked that, whilst an interface /can/ require implementers to > specify a default, in the case that they do not, it is still valid for > implementations to selectively elect to provide one. Therefore I can > append to your example the following case (I removed the `string` return > type for now): > > class ZipCompression implements CompressionInterface > { >     public function compress(string $data, int $level) >     { >         var_dump($level); >     } > } > > new GzipCompression()->compress('', default ?? 6); > new ZipCompression()->compress('', default ?? 6); > > In this case, we get the following output: > > int(4) > Fatal error: Uncaught ValueError: Cannot pass default to required > parameter 2 of ZipCompression::compress() > > I would like to fix this if possible, because I think this should be > valid, with emphasis on /if possible/, because it may be prohibitively > complex. Will update later. > > Cheers, > Bilge > I think I am missing something here. From my understanding we are *either* coding against the interface and then it should not be possible to use `default` at all as no default is set in the interface. So the fatal error is totally valid for me. *Or* we are coding against the actual implementations. Then it is totally valid IMO that providing `default` when no default is set in the concrete implementation of the function signature raises a fatal error. So in the example above it's either new GzipCompression()->compress('', default ?? 6); new ZipCompression()->compress('', default ?? 6); and it is absolutely valid that the second one triggers a fatal error as no default is set in the concrete implementation. Or it's something like /** @var CompressionInterface $compression */ foreach ([new GzipCompression, new ZipCompression] as $compression) { $compression->compress('', default ?? 6) } in which case it should definitely fail as the interface doesn't provide a default. Cheers Andreas -- ,,, (o o) +---------------------------------------------------------ooO-(_)-Ooo-+ | Andreas Heigl | | mailto:andreas@heigl.org N 50°22'59.5" E 08°23'58" | | https://andreas.heigl.org | +---------------------------------------------------------------------+ | https://hei.gl/appointmentwithandreas | +---------------------------------------------------------------------+ | GPG-Key: https://hei.gl/keyandreasheiglorg | +---------------------------------------------------------------------+