Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:125266 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 A06211A00BD for ; Mon, 26 Aug 2024 10:58:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1724670027; bh=rkGAB2ui8RZ6TDFTjaZueqwUxAhV7fBPVqNw11iIsd0=; h=Date:From:To:In-Reply-To:References:Subject:From; b=jSNR/kt9goGa2sHAgXh27jnZVMmfjkBfDc5LPuYfENS7qPwJpNRrdUT75eyf6iDZP gkwZc50FwqSMEMfdBn+p7uKswpoGnrr3TZmwqja8sUYPMwIdDM2K5Oykwe0bUS6bzR kiJaZ/LYX7gPU7eKX5654wn487qyXcD6GryMU0BpLVDKC14UcbBqhoMrFoa868124U SvJhH6vpw9j2U7O71GivZ37WrvDFezV5AnTPXi9CBUkpfivdaMDFRDoh2H80gbY+s3 5KpY0Ni26IfZE4hxtkAgFc+RZb2nr8JBQiZaNeWQPrUql7fuziGmgN4qMf7ny8tsa1 lxy9H4q0UPnyA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 96129180042 for ; Mon, 26 Aug 2024 11:00:26 +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.1 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from fout5-smtp.messagingengine.com (fout5-smtp.messagingengine.com [103.168.172.148]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Mon, 26 Aug 2024 11:00:25 +0000 (UTC) Received: from phl-compute-03.internal (phl-compute-03.nyi.internal [10.202.2.43]) by mailfout.nyi.internal (Postfix) with ESMTP id EC13A1390A2E for ; Mon, 26 Aug 2024 06:58:32 -0400 (EDT) Received: from phl-imap-10 ([10.202.2.85]) by phl-compute-03.internal (MEProxy); Mon, 26 Aug 2024 06:58:32 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rwec.co.uk; h=cc :content-transfer-encoding:content-type:content-type:date:date :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm1; t=1724669912; x=1724756312; bh=roDt9V3j3zSCDaMNAATicQPiF6O36Sx0IP4fkg5gDXU=; b= g0foKSEppY1l9st1T2p6uCNDjtla5SW4zg0I9MWvKIY+/HMrcpOon5bcJdFHBbxJ aen+jWHp1Uf17kthwvUvOQiiuyfNItIq55L9krC/OW/mBrX3WOs7DjSdYzgfui1E 9eKJGBUY5jQ84e91uisZxjSRqcEkFg1NUQDvmLckkcLynjbvgpdxjUATDcjyvM2L UVEZIt3uvUGTEWI+TEGxA91h5Bwfv+19UqHKro+kl4M2ktcjoSbcFqljujmIMn+F rtF0+S0kGj4NUJmJV51/xJkljk+NxMbllyCD/axhGX/M9UInmgbCGlGqHSatiR0j b8CHOvBdgLdUi+42VoiqTA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1724669912; x= 1724756312; bh=roDt9V3j3zSCDaMNAATicQPiF6O36Sx0IP4fkg5gDXU=; b=i WIS/0zplM4LfxMfdqkMsVWmqfBXuiynE7d3zkliRo8MBqFRTZSjVyJWuGNyBpHek Q1lazVqfpDEUlwMaFGl9yOg6wHT7Cb4EYKjNdA4R8+WFR+rVue1HqPN7DHUaa4Ep aCzC8Suo9IqYgomHfNPq6meO3WKPVxHPbPOujMDyrinPbuw9l+HZpEdvd3GssfHo wmY7Sk6vJXv1jw1h/yyzbggBbWzy8i+Y4qWSDbi2VoiQilnR52PKQPq1jwm6iYDu +sM9r/bLzrq0t9BX2i7piGi7pnC3q019sqWJkhRO/sIBZlaOQv14R6z2yRHTm22P lXFj+7zKSy4ZmY+3Ho/+Q== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddruddvkedgfeefucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucenucfjughrpefoggffhf fvkfgjfhfutgfgsehtqhertdertdejnecuhfhrohhmpedftfhofigrnhcuvfhomhhmihhn shculgfkoffuohfrngdfuceoihhmshhophdrphhhphesrhifvggtrdgtohdruhhkqeenuc ggtffrrghtthgvrhhnpeeileeihfffffevgeehveevvdeiteeuvdegudejieetkedtgfdt gefhhfdtlefgieenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfh hrohhmpehimhhsohhprdhphhhpsehrfigvtgdrtghordhukhdpnhgspghrtghpthhtohep uddpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtohepihhnthgvrhhnrghlsheslhhish htshdrphhhphdrnhgvth X-ME-Proxy: Feedback-ID: id5114917:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id A0B183C0066; Mon, 26 Aug 2024 06:58:32 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 Date: Mon, 26 Aug 2024 11:58:12 +0100 To: internals@lists.php.net Message-ID: <504ee8d4-fe96-4528-9f0c-5b23711c4b0b@app.fastmail.com> In-Reply-To: <15301ee5-7964-4f0b-8804-a19f95f43152@gmx.net> References: <0c8ed5d6-5507-4c41-8d7f-05d14ba8aa4c@scriptfusion.com> <15301ee5-7964-4f0b-8804-a19f95f43152@gmx.net> Subject: Re: [PHP-DEV] [RFC] Default expression Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable From: imsop.php@rwec.co.uk ("Rowan Tommins [IMSoP]") On Mon, 26 Aug 2024, at 10:03, Andreas Leathley wrote: > interface CompressionInterface > { > =C2=A0=C2=A0=C2=A0 public function compress(string $data, int $level)= : string; > } > > class GzipCompression implements CompressionInterface > { > =C2=A0=C2=A0=C2=A0 public function compress(string $data, int $level = =3D 4): string > =C2=A0=C2=A0=C2=A0 { > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 // do something > =C2=A0=C2=A0=C2=A0 } > } > > 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. This isn't unique to defaults; GzipCompression could also widen the type= to int|string, for instance, and there's no syntax for detecting that e= ither. If you have access to change class GzipCompression, you can resolve this= by creating an additional interface: interface SimplifiedCompressionInterface extends CompressionInterface { =C2=A0=C2=A0=C2=A0 public function compress(string $data, int $level =3D= 4): string; } class GzipCompression implements SimplifiedCompressionInterface ... Then, if we can agree an implementation, you could write: /** @var CompressionInterface $comp */ $comp->compress($myData, $comp instanceof SimplifiedCompressionInterface= ? default : MY_DEFAULT_LEVEL); If you don't have access to change the hierarchy, then what you're proba= bly looking for is structural typing, or implicit interfaces - i.e. a wa= y to ask "does this object meet these criteria". For instance, some imag= inary pattern matching on the signature: /** @var CompressionInterface $comp */ $comp->compress($myData, $comp is { compress(string, optional int) } ? d= efault : MY_DEFAULT_LEVEL); Note how, in both cases, we're not asserting anything about the default = value itself, only that the signature defines the parameter as optional.= It's actually a bit of a quirk that the interface has to specify a valu= e, rather than just stating this: interface SimplifiedCompressionInterface extends CompressionInterface { =C2=A0=C2=A0=C2=A0 public function compress(string $data, optional int = $level): string; } Regards, --=20 Rowan Tommins [IMSoP]