Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:122728 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 EE3FB1A009C for ; Fri, 22 Mar 2024 15:19:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1711120774; bh=VF9dzIjJbsnO9MEBAk1etkd/2cqYknYaTqbq0EDGQKc=; h=In-Reply-To:References:Date:From:To:Subject:From; b=DR5JXJdINED+VcRjDCBtXnqfeAHgXsuJzjxZVJCnhtq7MYY5wCdjrf7pOByN7U6kM 3+GJ0CLIJhxsBZitbfPKcQpBVFN1/DCwlGLIkFUcWiz3fLJkyLbyaQdYLSpbU/MuVz z1qjHf5BwAVqRgCsL4Y16XrYq6ihcqMAMRgMV/dWA9G9RrnjbFVNX6TIbQ/ZciwCbX TbQx+mDZlfdq+S1Eyfw7M6YfzfYM4D4BsQIRtIqLeqFnc9pxOsuHP3S5NrB6xS2Ibg vwfwqhpngzww4hdAg1tE2Ivp6S5lUFzRX/11/N2GvMLX+FgWwiq+mIQEjunx4C/qjS BtKq78z9HWwZg== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 4947518005B for ; Fri, 22 Mar 2024 15:19:33 +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, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from fhigh4-smtp.messagingengine.com (fhigh4-smtp.messagingengine.com [103.168.172.155]) (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 ; Fri, 22 Mar 2024 15:19:32 +0000 (UTC) Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailfhigh.nyi.internal (Postfix) with ESMTP id AD82911400CB for ; Fri, 22 Mar 2024 11:19:09 -0400 (EDT) Received: from imap50 ([10.202.2.100]) by compute6.internal (MEProxy); Fri, 22 Mar 2024 11:19:09 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rwec.co.uk; h=cc :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=fm2; t=1711120749; x=1711207149; bh=40yvi4E7O6 ceJC0EjZovo7U79l8qwZwX9DiJMDJmivQ=; b=CuzSdSThDf37OT8Biuy+ET/7Yg RR3Awgd06qWDvcY8ZHj0Iro4M6rn9fzNFXMayFlQYuctKUPierAhWSwcO6JRKz2U CQA2Ol6x9y0FjcfR1xLANPZxqtZhs/E5fjvI8BgzvDkAqoSoYGzfPJbRkV2xGVF/ JPvLqsrWH/o8MOjw4158AeX9kP0xQoAH+2KJgYoY4gJHQdPpSOj1NQ0+kcHZ/m2N hqvjmjz7pr31spIV/iYiowmoT+3XI20S2X8EviJxd+auFlI2l80oXx/ONYeolId7 M0T++FN62msXB6PAFhuKpmrIj0w2/THea47R5kRD1ewwVQavUIOfiI5AMeIg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc: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= fm2; t=1711120749; x=1711207149; bh=40yvi4E7O6ceJC0EjZovo7U79l8q wZwX9DiJMDJmivQ=; b=EgWAIBgMSdWOmBUdw5Js4RvGVotWJX7+D+H7ODvftShm SNM1hWbdkv3vjefDuS5FFo3gMlxUJamF46J6Ko8pRf603C5BTg59A09mNFe+gclv ThV+gmBiJ2ezGxP44L9Z5hdpNdiduUBCGFINpUCiDXfra+Bqc8z9ACV3UGiwvzgW 4sNpxy1Zz77BBeRFJuH86mJPWKBOIAUA0RA02+mhl6z9hPeJ4itb7ZbO8t5xxBy+ vMYIFEqhfF5UbLz8PWT+5rUL8j3VMZXyO783VA3rPinL9EPl1C5FWnpbRqI4LcgC zGplMk8m9X250HvXQ1cLPXVhA85+MdHFm7f4DgNj+Q== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledruddtvddguddtucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefofgggkfgjfhffhffvufgtsehttd ertderredtnecuhfhrohhmpedftfhofigrnhcuvfhomhhmihhnshculgfkoffuohfrngdf uceoihhmshhophdrphhhphesrhifvggtrdgtohdruhhkqeenucggtffrrghtthgvrhhnpe dtudeiieffleevtdduteeuffegteevteduueefgfekvdekffeitdfgveeuveehueenucev lhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehimhhsohhprd hphhhpsehrfigvtgdrtghordhukh X-ME-Proxy: Feedback-ID: id5114917:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id 2B79F1700093; Fri, 22 Mar 2024 11:19:09 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.11.0-alpha0-332-gdeb4194079-fm-20240319.002-gdeb41940 Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 Message-ID: In-Reply-To: References: <3F78A125-1946-42E2-A4F5-A2B282BE2107@rwec.co.uk> <2d7ec203-6e80-445c-94f4-d29ef58743b1@rwec.co.uk> <7d53f2e4-46f3-46f1-89b0-6e1d3b0b2e32@app.fastmail.com> Date: Fri, 22 Mar 2024 15:18:46 +0000 To: internals@lists.php.net Subject: Re: [PHP-DEV] Proposal: AS assertions Content-Type: text/plain From: imsop.php@rwec.co.uk ("Rowan Tommins [IMSoP]") On Fri, 22 Mar 2024, at 12:58, Robert Landers wrote: > >> $optionalExpiryDateTime = $expiry as ?DateTimeInterface else new DateTimeImmutable($expiry); > I'm not sure I can grok what this does... > > $optionalExpiryDateTime = ($expiry === null || $expiry instanceof > DateTimeInterface) ? $expiry : new DateTimeImmutable($expiry) Trying to write it as a one-liner is going to make for ugly code - that's why I'd love to have a new way to write it! But yes, that's the right logic. With the "is" operator from the Pattern Matching draft, it would be: $optionalExpiryDateTime = ($expiry is ?DateTimeInterface) ? $expiry : new DateTimeImmutable($expiry); But with a clearer assertion that the variable will end up with the right type in all cases: $optionalExpiryDateTime = $expiry as ?DateTimeInterface else some_other_function($expiry); assert($optionalExpiryDateTime is ?DateTimeInterface); // cannot fail, already asserted by the "as" > Maybe? What would be the usefulness of this in real life code? I've > never written anything like it in my life. I already explained the scenario: the parameter is optional, so you want to preserve nulls; but if it *is* present, you want to make sure it's the correct type before proceeding. Another example: // some library function that only supports strings and nulls function bar(?string $name) { if ( $string !== null ) ... else ... } // a function you're writing that supports various alternative formats function foo(string|Stringable|int|null $name = null) { // we don't want to do anything special with nulls here, just pass them along // but we do want to convert other types to string, so that bar() doesn't reject them bar($name as ?string else (string)$name); } To put it another way, it's no different from any other union type: at some point, you will probably want to handle the different types separately, but at this point in the program, either type is fine. In this case, the types that are fine are DateTimeInterface and null; or in the example above, string and null. > $optionalExpiryDateTime = $expiry == null ? $expiry : $expiry as > DateTimeInterface ?? new DateTimeImmutable($expiry as string ?? "now") If you think that's "readable" then we might as well end the conversation here. If that was presented to me in a code review, I'd probably just write "WTF?!" I have no idea looking at that what type I can assume for $optionalExpiryDateTime after that line, which was surely the whole point of using "as" in the first place? Regards, -- Rowan Tommins [IMSoP]