Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:125296 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 867241A00BD for ; Mon, 26 Aug 2024 22:09:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1724710286; bh=yTcvxEmsv66c5OY7ziuRc7gacuwmOjGCsDkOrJ4Ycfg=; h=Date:Subject:To:References:From:In-Reply-To:From; b=YECtFNse7aFrL9kDzB9C/e94YJRs7e01/74ybgV5xfwj5wS0/+autHLTs4a6lbGBs 8Q0xXVUDhtyc809giAsC9Jzzc/7t7GEIh1qd7ToR4liyzUVlWBrPBApkY5pPrLsmYn yAJGZUY3Bmf0pVaB7aDwW/wmxDr0Qi3NgeqegK2R4mY2aaGfwntlcCN26TtkAI610B 9VcBp+89/26GVNQipvzzUyB71jO85t06fkKPqklRWYWJsEWmlkOJ96oiZhZoyCQRTg QMbSSCLLAOfNNii8cgwOGi0EMtqnrO03bAYnp6mPbBJWMwK8dadgsUoF7TJpK0yctC mZtKnnGpKiEtQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id C4D64180084 for ; Mon, 26 Aug 2024 22:11:23 +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.8 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DMARC_MISSING,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2, SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-wm1-f51.google.com (mail-wm1-f51.google.com [209.85.128.51]) (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 22:11:21 +0000 (UTC) Received: by mail-wm1-f51.google.com with SMTP id 5b1f17b1804b1-4280ee5f1e3so41874805e9.0 for ; Mon, 26 Aug 2024 15:09:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=scriptfusion-com.20230601.gappssmtp.com; s=20230601; t=1724710167; x=1725314967; darn=lists.php.net; h=content-transfer-encoding:in-reply-to:from:content-language :references:to:subject:user-agent:mime-version:date:message-id:from :to:cc:subject:date:message-id:reply-to; bh=QmXEbqe5J5oaswxxS6uvbDesRLfVWQqhD8CUc0Bir0o=; b=GVDYkHRDwQMRCCvmNwNPQRMc/ax0/f0X1GKYlAVEg+5cZMBPa5d2+Y/TDJzj/DAen+ s05tQ4bytq1d7vbHNfw5ytfIHCkJndkagBYulTqkJYjwLfynOdnZ7IPRmlBAh2Z0lrOS 2UxTfaPvTjDVn8JLUccZ5vmKZq4SkhS0twqw9ExZMLxOp+QjWXhsJQ9APr4fPF3+XFAx xGVOLTnt3KEh+MvIDMAZWSB1mxdUcU21mA5AU9rJjKH2lf/TvRNDPO7pi39kWIJLx9qi bHmzCYXwIn65YXUMrG/cv/aONjMmQ44JueTymUHKKY41undDHZiXQTa9PbsTkm+bTLy6 T3jQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724710167; x=1725314967; h=content-transfer-encoding:in-reply-to:from:content-language :references:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=QmXEbqe5J5oaswxxS6uvbDesRLfVWQqhD8CUc0Bir0o=; b=Vl+L0RkCe1Ea3abEdhR6LZ8Qq0LF4nZCQczp/1j49tpSQuXYRQpaH9tujkSd713cf8 gfJVy5dvuUaUkkpObEuIM6MUaohxM2rNnedh9KjO+4YzGQ7zOHh+fTAmcR13snQh82aO x8pHtjeG5EnzD18i4fn2SMEazRfqrYuEZAlAYzSvROkEPwsJ5fm6qAOcpD6w3hG1F9ea VKJBDRTvTAmw0MU1194fV+TrBVvZlk7zJ/3O5Td4fuwtshtbQAXHsfjC9nPquMayYjEO BhNZVV6gYk1z3j+dUjRj5ExxOHzgAYeZe3KbCxkGJVdxj/27pWReN3dyUeD4xiHBoLlJ Aa3w== X-Gm-Message-State: AOJu0YxG3O1WowrF8fp0c7ss3sFRh2xrdZLmvNnlS/abrxjZ6JwtHi/l RTlBOijjGyPWGQAxwUA3/F7U/y7SN84kx4Fr5RKcjOBciWnntzS6uC/w0q1a0UhzHwSvUzkLUT5 g X-Google-Smtp-Source: AGHT+IHdtZk/W22cI/2E0ObjffyQU5GpUur+tEG10C7BabwX0WyWGgArl7XlHNCJAy9mx+jVc8/P2g== X-Received: by 2002:a05:600c:3553:b0:428:1310:b6b5 with SMTP id 5b1f17b1804b1-42b9ae4745emr4858655e9.34.1724710166833; Mon, 26 Aug 2024 15:09:26 -0700 (PDT) Received: from ?IPV6:2a01:4b00:bf09:5101:f1f9:cef9:e305:d7f2? ([2a01:4b00:bf09:5101:f1f9:cef9:e305:d7f2]) by smtp.googlemail.com with ESMTPSA id 5b1f17b1804b1-42ac624bb23sm165464925e9.32.2024.08.26.15.09.26 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 26 Aug 2024 15:09:26 -0700 (PDT) Message-ID: Date: Mon, 26 Aug 2024 23:09:25 +0100 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> <5c980153-228a-4544-8fb7-09a1c265ef14@heigl.org> Content-Language: en-GB In-Reply-To: <5c980153-228a-4544-8fb7-09a1c265ef14@heigl.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit From: bilge@scriptfusion.com (Bilge) On 26/08/2024 11:32, Andreas Heigl wrote: > 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. Hi Andreas, Thanks so much for pointing this out. Your argument seems absolutely correct to me, that this is an invalid polymorphic pattern, and as such, I have stopped development of this feature. I still want to thank the other Andreas (L) for raising it, and intend to add it to the possible future scope in the RFC to provide an exception to permit `default` on the LHS of null-coalesce. We did at least assess that such an exception would be viable even if the development work would be disproportional to the benefit, so it's worth noting that down. Cheers, Bilge