Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123329 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 9CA781A009C for ; Fri, 17 May 2024 08:37:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1715935089; bh=ZVHvNG4yg0cH5yPvjEpDE2+7PGfQC8q+7NIVp+Xau8o=; h=In-Reply-To:References:Date:From:To:Subject:From; b=QFYMBOIhD5Wk3gl7Ghf0Qj7ZusiYaC6wcatpItMqBAdbiJa6A8T3+/q1LHyGZHSZv pmBEiPtP5PkRU7D1cIejxSiaQGuPfBO99ismNtd1Gw8fB03ONr6m8HPmmVRZXvT0DK +gCQpsBFZyl/Tt5epx1V8BnRaTSPW2aUWf5Ghf+naPXlDZXSA2iNkAVNnLFu60Rtyk 85se2P146yUPNSeFF0pr9qo12za6j8T6SmEAuRDeM2rHEl9wuh4+vLRvVZmWVDXHAl RqCZoG78h4ShevmULSobyVxyswuuP//OisO/cZ8P9ttzBkwL5Iak8wo4yaK0z9mt2j /Gv7ykgNkOsIw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id EED07180057 for ; Fri, 17 May 2024 08:38:08 +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=4.0 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DMARC_NONE,HTML_MESSAGE,RCVD_IN_DNSWL_LOW,SPF_HELO_PASS, SPF_SOFTFAIL,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: Error (Cannot connect to unix socket '/var/run/clamav/clamd.ctl': connect: Connection refused) X-Envelope-From: Received: from fout3-smtp.messagingengine.com (fout3-smtp.messagingengine.com [103.168.172.146]) (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, 17 May 2024 08:38:08 +0000 (UTC) Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailfout.nyi.internal (Postfix) with ESMTP id 86A3A138148F for ; Fri, 17 May 2024 04:37:14 -0400 (EDT) Received: from imap50 ([10.202.2.100]) by compute3.internal (MEProxy); Fri, 17 May 2024 04:37:14 -0400 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= fm3; t=1715935034; x=1716021434; bh=ZVHvNG4yg0cH5yPvjEpDE2+7PGfQ C8q+7NIVp+Xau8o=; b=fEd0FBv33YteaFC4DwI/TZk23fZOVF+sOdvlbdrCjQCS 2/gQU8uknykauvLYOCus3/LuymXm1GFDnh4UAt7BdtTOCCXsgLky4GCA7+PWo0Er XFNCS2VQoRBBe02W9tM+6FlVIJzTyA27QGiE+9/bwd49Ffb6qVnU9ThFJupE6zn1 bZbr48/UbFTHxq0O4x7oNf7j4+7WKQjfDhCReD0ez9KnXN/JgA4DinrUmslhBXCn pMdROgCjYXItJb+3suIQjY0rZbQBGFuCLBCzS3BAVzYqm939J+/cpLjInOqXUopx hF3WS2zhLmrdr8l8k8Ugbivf8S2YVRsrS1mcAcu1Dw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrvdehfedgiedtucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefofgggkfgjfhffhffvufgtsegrtd erreerreejnecuhfhrohhmpedfvegrshhpvghrucfnrghnghgvmhgvihhjvghrfdcuoehl rghnghgvmhgvihhjvghrsehphhhprdhnvghtqeenucggtffrrghtthgvrhhnpeegffetke eggeeiheffleejfeelffehleehkeevheeliefgvefffeekffdvfeejfeenucevlhhushht vghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehlrghnghgvmhgvihhjvg hrsehphhhprdhnvght X-ME-Proxy: Feedback-ID: id4f946ef:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id 303A21700093; Fri, 17 May 2024 04:37:14 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.11.0-alpha0-456-gcd147058c-fm-hotfix-20240509.001-g0aad06e4 Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 Message-ID: <701a5bdc-d2c5-4527-b54f-1ae092f13b38@app.fastmail.com> In-Reply-To: <3A6.1T4AB.9eXWvq4hof.1cHcqE@seznam.cz> References: <3A6.1T4AB.9eXWvq4hof.1cHcqE@seznam.cz> Date: Fri, 17 May 2024 10:34:42 +0200 To: internals@lists.php.net Subject: Re: [PHP-DEV] [RFC] [Vote] Type Guards for Classes Content-Type: multipart/alternative; boundary=132fdb8355034d7596e2e41bc65b88f4 From: langemeijer@php.net ("Casper Langemeijer") --132fdb8355034d7596e2e41bc65b88f4 Content-Type: text/plain;charset=utf-8 Content-Transfer-Encoding: quoted-printable On Thu, May 16, 2024, at 22:31, Patrik V=C3=A1clavek wrote: > This feature aims to simplify and standardize the process of verifying= that a variable is an instance of a specific class, enhancing code read= ability and reducing boilerplate code. >=20 > Currently, in PHP, to ensure that a variable is an instance of a speci= fic class, developers need to use the `instanceof` operator and manually= throw an exception if the check fails. This results in repetitive boile= rplate code scattered throughout the codebase. A new syntax, `(ClassName= ) $variable`, is proposed to streamline this process by performing an in= stanceof check and throwing a `TypeError` if the variable is not an inst= ance of the specified class. I view variables changing types to be an anti-pattern. Instead if adding type guards, I would rather see this problem solved by= having typed local variables. This ensures that a variable always holds= a specific type, avoiding the need of repetitive scattered type guards.= Working with variables would then be the same as working with class pro= perties. They could be typed or untyped. Also, I see the need for type guards as a symptom of bad composition. Mo= stly methods that are too large would benefit. If a method is decomposed= into smaller methods the use of parameter and return type checks it's u= nlikely there is need for additional type guards. --132fdb8355034d7596e2e41bc65b88f4 Content-Type: text/html;charset=utf-8 Content-Transfer-Encoding: quoted-printable
On Thu, Ma= y 16, 2024, at 22:31, Patrik V=C3=A1clavek wrote:
This feature aims to simplify and= standardize the process of verifying that a variable is an instance of = a specific class, enhancing code readability and reducing boilerplate co= de.

Currently, in PHP, to ensure that a var= iable is an instance of a specific class, developers need to use the `in= stanceof` operator and manually throw an exception if the check fails. T= his results in repetitive boilerplate code scattered throughout the code= base. A new syntax, `(ClassName) $variable`, is proposed to streamline t= his process by performing an instanceof check and throwing a `TypeError`= if the variable is not an instance of the specified class.

I view variables changing types to be an a= nti-pattern.

Instead if adding type guards,= I would rather see this problem solved by having typed local variables.= This ensures that a variable always holds a specific type, avoiding the= need of repetitive scattered type guards. Working with variables would = then be the same as working with class properties. They could be typed o= r untyped.

Also, I see the need for type gu= ards as a symptom of bad composition. Mostly methods that are too l= arge would benefit. If a method is decomposed into smaller methods the u= se of parameter and return type checks it's unlikely there is need for a= dditional type guards.
--132fdb8355034d7596e2e41bc65b88f4--