Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:122725 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 A01191A009C for ; Fri, 22 Mar 2024 11:58:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1711108757; bh=MOBIShTK1nWTDwsA3iyHiSWen85qL+mLQjr+KOmYUYk=; h=In-Reply-To:References:Date:From:To:Subject:From; b=SJ6DSHr1u9Cl3vM/0mWWv7x8k6GK4VJeMIXSLPv+ao92qo7rtjKnNls2hCZHdEPn9 MV/DsEq+lJxO+fqXmSifAxW6GTJIcbHFDt9QP5uHqrkVFKGKf27XSIVDq7d6YpMeQO gWDbQfTwcBz7mcddLxP+sQ8jQbObpWWo78aB1ssk9ob+co+yGnyAcWOk+uV7Wdfiet 9TasH6d5HS4X5xZ2WsumkeRmsZ4QQSSVRL3mpwRwgvYGOErSE+CCDLaMw6hlo0gO6W eoGU5GpsOGUNf1DZ/Ts4Jg5xiD12jB4hNharmw1DvFf0VSw3jDH5ob8PZyQY3QaYul 9j/qMaod9IkPQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 41143180077 for ; Fri, 22 Mar 2024 11:59:16 +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_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,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 out2-smtp.messagingengine.com (out2-smtp.messagingengine.com [66.111.4.26]) (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 11:59:15 +0000 (UTC) Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailout.nyi.internal (Postfix) with ESMTP id 51CA35C0089 for ; Fri, 22 Mar 2024 07:58:53 -0400 (EDT) Received: from imap50 ([10.202.2.100]) by compute6.internal (MEProxy); Fri, 22 Mar 2024 07:58:53 -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=1711108733; x=1711195133; bh=0t2VZoTQnW A8FKFUsssndGCNnxRyNWTapFKIekhQyLQ=; b=SGyOfwiLdYBsGQMJNtq0VUMf3+ fWyBAAe9BxkHZgfnwn5Cd3ZLvNuuN0LlItGYi7mYQiYLtumPwQI4Hex2RZ8daNts ZoPXs3u/7rL2Rvb/FHHySzSjYVENBDs3sUWCpuaq3tvGwZYwMXViSz1pLOt71PJk koPsj+E5WCMspTeL7lihdDPc91c+VreRQ9emwBPcrOD85pABl8qR1Bz+avyi+7LL eUQpzDkwfhfUZzZP/hVBl9ipcdET32IMs9XhT48CUa74x2DM8OiTBQ9Wgr730eEQ dxnayPaiBpXhldKnWM6ukUXaSTxz2/ZuwTxYaHXaxcyjOZQQWMpwXPWd1bXQ== 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=1711108733; x=1711195133; bh=0t2VZoTQnWA8FKFUsssndGCNnxRy NWTapFKIekhQyLQ=; b=XEmQjUGQaMmVqhReVXSr0w3qCcze3UlhOLS6HjpskZxE 9FLdEBoDjF81LMzISEx2AbkW38Bol+31pYKwVFJCzgXk1Q13/1tKXFZJ43s7S0o7 wv+plwPEAGlNeYVTxO1NvyQxIDL4JypklVIY4YCcjkyYwFDp/8PWp1p3oPiEbxUl IIVD/LiK3XdRs6aAY2p9iL5pQFEze4XBkAQ/y5NanMWkF8E6SaAqMFxMNMvOCsDu LyV/ETaSXmvnZDIUTFWffvEYbUumYMaOoxAkRMwT3kHUU+/IdYdt7M9frhESC3pJ LSag61GRcBkqYmdCfy6drOb4KX2C2pdjF4ijh1RHjA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledruddttddgfeduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefofgggkfgjfhffhffvufgtsehttd ertderredtnecuhfhrohhmpedftfhofigrnhcuvfhomhhmihhnshculgfkoffuohfrngdf uceoihhmshhophdrphhhphesrhifvggtrdgtohdruhhkqeenucggtffrrghtthgvrhhnpe egtdeiheehhedthfegffdtheffleevfffhvedujeegveetgeevgfegtddvueetheenucff ohhmrghinhepmhhitghrohhsohhfthdrtghomhenucevlhhushhtvghrufhiiigvpedtne curfgrrhgrmhepmhgrihhlfhhrohhmpehimhhsohhprdhphhhpsehrfigvtgdrtghordhu kh X-ME-Proxy: Feedback-ID: id5114917:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id E34A81700093; Fri, 22 Mar 2024 07:58:52 -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> Date: Fri, 22 Mar 2024 11:58:30 +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 10:05, Robert Landers wrote: > After asking an AI for some examples and usages, the most compatible > one would be C#'s. In actuality, I think it could be hugely simplified > if we simply return null instead of throwing. There'd be no special > case for |null, and it would move the decision making to the > programmer: > > $x = $a as int ?? throw new LogicException(); It might be relevant that C# has only recently introduced the concept of explicitly nullable reference types, with a complex migration process for existing code: https://learn.microsoft.com/en-us/dotnet/csharp/nullable-migration-strategies So in most C# code, there isn't actually a difference between "expect a DateTime" and "expect a DateTime or null" PHP, however, strictly separates those two, and always has; so this would be surprising: $x = $a as DateTime; assert($x instanceof DateTime); // will fail if $x has defaulted to null! That's why I suggested that with an explcit default, the default would be automatically asserted as matching the specified type: $x = $a as DateTime else 'No date given'; // TypeError: string given, DateTime expected $x = $a as DateTime|string else 'No date given'; // OK $x = $a as DateTime else null; // TypeError: null given, DateTime expected $x = $a as ?DateTime else null; // OK If the statement runs without error, $x is guaranteed to be of the type (or pattern) given to the "as" operator. Regards, -- Rowan Tommins [IMSoP]