Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:125259 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 C9B341A00BD for ; Mon, 26 Aug 2024 09:26:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1724664532; bh=S9JseBnT2Ey1RH/9KAQ6N3iGMjVsEaCI+AinGylmsXE=; h=Date:Subject:To:References:From:In-Reply-To:From; b=P+TXdOa5LI4RO9DNjB8urGyKSsRPpgttCENSnDP3DKQrCvJu11OwX1k5NVZ7oYzjK yOzb0lZLDb3tAjsRDmqQ9hR09RLrSBbrKBuAFKwEc1W+qllLuIXXNly/xe587N3Q4d GA1D2HDu90ILcEVMD0t2rX7juQDM2rSoXwe7woMJ0yvVu5odGcWiSpwp0TjCUqIsiz YeoTk9YX2MqOuPwfcZ9tQy/kdD/TsYKwACCTL+M9XYHedhUsj0FLmSHPEmzVXXlEW7 JPRB0cjR1HpzryJgAVuR6IqXScPp8mb8qvHT8nX2/lUYjRrm/wp5exoAWAafJi3sh0 tBfIZDAkaPLaA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 6F5D8180042 for ; Mon, 26 Aug 2024 09:28:51 +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,HTML_MESSAGE,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-f49.google.com (mail-wm1-f49.google.com [209.85.128.49]) (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 09:28:50 +0000 (UTC) Received: by mail-wm1-f49.google.com with SMTP id 5b1f17b1804b1-4280ca0791bso36519535e9.1 for ; Mon, 26 Aug 2024 02:26:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=scriptfusion-com.20230601.gappssmtp.com; s=20230601; t=1724664417; x=1725269217; darn=lists.php.net; h=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=blPAhAjjIjB/fSvlQzzXoynyPzoFjL/8qf4Mh4Ta8ck=; b=lbyOTy+uH7Yie1NeLTOFp0fNI+dleQoLmZ1nkJBAlJ1I2urVb4hzAbKsdd93avBOXH UTZOWMi4QjYnplm0Mc1+HkTxlUhwxggkFSFssYrZpbEuLkoEOWkqdcA/ZjoJ34hBLFMX lYOxHFtipsKCm8lLxoDdsoP7kKaqrKVRWRkgGrpYwwwp97V2t+B4ebXru4Gf409GbwdO zH0E2LeOqNIwpWgV/2DklPosOERxxp0rRVJ05EVElL7zNdWaBl92oknSqpFaGS+9o8Ma QXJTULDRtw81uSIwzFeMY8xi6U/SiKj5hE6WqmF5TVb1+QJpWmRTrjAgsKheML1gWRth MALw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724664417; x=1725269217; h=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=blPAhAjjIjB/fSvlQzzXoynyPzoFjL/8qf4Mh4Ta8ck=; b=tkP9IqnK928CDz2QagdQ7O3ij7uNQ2pqdOg8XPi83q5h0KFBynYSyZyeo2GNFXXySv cWQrbc81Qs07aMvcAbVdq5FFFA/jcff0K+YjqvEK2xyQrhOlTM0TOOFd7YPCWs553u0R T5HtSP0msHUXmdATrXxspyg94Vg09zoYOqzIkv88Qt2+Owqxtgpt37jEZkcV99uNSAdH ml/wyh4EFYTSLIdu9QxVRsmiKZEDHld6bszFaKS4XGnlmart2X8Uw6el1tcFwZi0W8KI Ogp3+kymzvcnYPIuTXme5ky65E9gC20TSJq+5XI/SJ5nMVwWuv1YdUEWBpAexRqy5FRL j1bA== X-Gm-Message-State: AOJu0Yxr10FWaH9usxwZF3875o0ablhwwrFD+5rfyrUoIU8bwd85gb8m LNgbOxMJsar2WzENpKYrRq1SRcMQflg1jC/ZFsMAZN1VA84VCV9p7YfKN1rdPALq2gBbhnWibCA p X-Google-Smtp-Source: AGHT+IE8vmRUZuKkta9rF7+Lp0QEUY5seoS0cLfyp29eID7ZJPM7BbbHtc4BKPZ8T1e4xd+HS/yziA== X-Received: by 2002:a05:600c:1d10:b0:426:68dd:bc92 with SMTP id 5b1f17b1804b1-42acc8d361emr66372235e9.5.1724664416698; Mon, 26 Aug 2024 02:26:56 -0700 (PDT) Received: from ?IPV6:2a01:4b00:bf09:5101:24c4:e0bf:99bf:8460? ([2a01:4b00:bf09:5101:24c4:e0bf:99bf:8460]) by smtp.googlemail.com with ESMTPSA id 5b1f17b1804b1-42ac5180339sm146225915e9.43.2024.08.26.02.26.56 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 26 Aug 2024 02:26:56 -0700 (PDT) Content-Type: multipart/alternative; boundary="------------LUUITnlHEQ15yUtwlHNJKF2r" Message-ID: Date: Mon, 26 Aug 2024 10:26:55 +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> Content-Language: en-GB In-Reply-To: <15301ee5-7964-4f0b-8804-a19f95f43152@gmx.net> From: bilge@scriptfusion.com (Bilge) This is a multi-part message in MIME format. --------------LUUITnlHEQ15yUtwlHNJKF2r Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit 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 --------------LUUITnlHEQ15yUtwlHNJKF2r Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit
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

--------------LUUITnlHEQ15yUtwlHNJKF2r--