Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123328 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 B9BBA1A009C for ; Fri, 17 May 2024 00:12:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1715904779; bh=uo6tOJFcr6Bw1hPtXhzrrWEebJYvrud6jQpIu2X1etI=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=The9YsgUj/w57XYwA92sHJzuZCyzKA6o3R66TqOHZ+cZL0y0c1Yc5jfsP7+YoofLM hoXZsmc25sVXWp74f2UIIZozJkcQJzNQRgpoUnH+SlUqnwjEFJqW3j/8YWns64Htga 8f8pZnLGY1rBjleGZ/FaO+VKwNU8AoJMDStPTVZ2jdsapToENQxmQaggkX+SX6Zp+q J0V5qZAlfKZKU7JIZa539xBTaTPemHmEhDefGlmu4RjxRmu9id//nhNF8DYv7lL+ST P/5zQWrOpiplQoofdr5nBtW6KBQlZF5KU7ZOk3gvFzNdmUdVExu3thd9PwoFDSpSZe CV8s8SG+xrlRw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 5D06E180079 for ; Fri, 17 May 2024 00:12:57 +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.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_PASS,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 mail-pj1-f43.google.com (mail-pj1-f43.google.com [209.85.216.43]) (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 00:12:56 +0000 (UTC) Received: by mail-pj1-f43.google.com with SMTP id 98e67ed59e1d1-2b9702e05easo662914a91.1 for ; Thu, 16 May 2024 17:12:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715904722; x=1716509522; darn=lists.php.net; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=05tjIGqfkSDEq8g8LX9inaylP3O6SLcjofrY/DFM+2U=; b=Vi1GojRIMUAoo4YXrwBv+jZbPBFQTJtLBuG6pATBgtDVRQVwE5fibc2dsjBsc/wzsp Iws0Ot+tfHSXvEnsnpINpz0CKTGFWcfKn9NtZkeMY8ojb5knW53dioGB4Kw8uEblFpRH 31M/UQG1gdbuKR1JeqUYPenKwvHj3B3qL1t2dOpa9LFQqxcjbK+udcKrqkKLPLDc3ziY YMmojijTJKdIIvZ7ve3vzTxcflAiSV/GinKcDrxQb8M1E6GY+NjuhAyJvbbt2H3Gt+XM suG9hWT/3s++ZH3p1b+U3uIbZxVvcf3j041S5Tf5nsMXNg4DsTkMbuu+8sSaiOYGfLUa YSnQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715904722; x=1716509522; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=05tjIGqfkSDEq8g8LX9inaylP3O6SLcjofrY/DFM+2U=; b=Gu58bwTEbObauyrW9Pr7Zp3BNuoF2zs8ezSH+vwR0/+2xwuKH+QFKqaRWbXg6gCY32 jwapVt0Qyd3l4HhqvdNyBiV9nhjNBpmjHbBfbzxpP7/WXB3xhG9mtfx0yr9AzHHymSz/ 0tKst3HgZbIb7Om02rQqHo+SlEihhJvWsc6GENqvnwwe8zuEvNYWxFMITia78WDbvzS8 45NAjq7zlSpv5FT0P294SmMLYHizD7BP2B6fJUexk1XLLeaJ822rbkBOszrj6ZiooQ6d h8LwSPUXe7c27z3nHdvvrdMC4AlwXF0SfmZ3ycVb1oOr5ZH/+BtGB/xUNE7CQ4eFdHKN /GKw== X-Gm-Message-State: AOJu0YzfNK0mD9HS20ErWxbpfptUEMoAhfW8JMwirBfr7v1G4n4Y0eV7 bLxKhF0eDg/YYN9cfGA+ZuNnfBasndGPcDyXofv3uHQc6qidy0VVRXTsSuVZdedkU+8yTA6Co3P nzwLF+lTfEvWn13WOPFQpPgmNI2k= X-Google-Smtp-Source: AGHT+IGnCFYXUXKbQEL4azCASKq4ottgEOuklFtQuEbIN5sVQODnJUyR3QjG95cEu+v14eT2GPgHIQ/pm/rz2s+DhaU= X-Received: by 2002:a17:90a:17a3:b0:2b5:af18:5f8c with SMTP id 98e67ed59e1d1-2b6cc342849mr16492007a91.13.1715904722436; Thu, 16 May 2024 17:12:02 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 References: <3A6.1T4AB.9eXWvq4hof.1cHcqE@seznam.cz> In-Reply-To: <3A6.1T4AB.9eXWvq4hof.1cHcqE@seznam.cz> Date: Thu, 16 May 2024 17:11:50 -0700 Message-ID: Subject: Re: [PHP-DEV] [RFC] [Vote] Type Guards for Classes To: =?UTF-8?Q?Patrik_V=C3=A1clavek?= Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary="000000000000730bc506189b332d" From: jordan.ledoux@gmail.com (Jordan LeDoux) --000000000000730bc506189b332d Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thu, May 16, 2024 at 1:32=E2=80=AFPM Patrik V=C3=A1clavek wrote: > Introduction > ************* > > This RFC proposes a new feature in PHP: type guards for classes (or > interfaces). 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 code. > > Motivation > ************* > > Currently, in PHP, to ensure that a variable is an instance of a specific > class, developers need to use the `instanceof` operator and manually thro= w > an exception if the check fails. This results in repetitive boilerplate > code scattered throughout the codebase. A new syntax, `(ClassName) > $variable`, is proposed to streamline this process by performing an > instanceof check and throwing a `TypeError` if the variable is not an > instance of the specified class. > > Proposal > *********** > > Introduce a new type guard syntax for classes: > > ```php > (Foo) $variable; > ``` > > This syntax will internally perform the following operations: > > 1. Check if `$variable` is an instance of `Foo`. > 2. If the check fails, throw a `TypeError` with a message indicating the > expected and actual types. > > Example: > Consider the following class definition: > > ```php > class Foo { > // class definition > } > ``` > > To ensure a variable is an instance of `Foo`, instead of writing: > > ```php > if (!$variable instanceof Foo) { > throw new TypeError('Expected instance of Foo, got ' . > gettype($variable)); > } > ``` > > Developers can use the new type guard syntax: > > ```php > (Foo) $variable; > ``` > > Backward Compatibility > *************************** > > This feature introduces new syntax and does not affect existing code. It > is fully backward-compatible, as it does not modify or deprecate any > existing functionality. > Since this throws, I'm struggling to understand how this would replace any usages of `instanceof` other than `if (!($var instanceof Foo)) throw new TypeError();` Also, this is not an RFC with a page I can look at OR something in a vote, so the subject line is a bit of a lie. But now that I've gotten my grumps out of the way, better class specific syntax is in general something that I think is positive and worth exploring= . Jordan --000000000000730bc506189b332d Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


=
On Thu, May 16, 2024 at 1:32=E2=80=AF= PM Patrik V=C3=A1clavek <PaTrOnY@ema= il.cz> wrote:
Introduction
*************

This RFC proposes a new feature in PHP: type guards for classes (or inter= faces). This feature aims to simplify and standardize the process of verify= ing that a variable is an instance of a specific class, enhancing code read= ability and reducing boilerplate code.

Motivation<= /div>
*************

Currently, in PHP, to = ensure that a variable is an instance of a specific class, developers need = to use the `instanceof` operator and manually throw an exception if the che= ck fails. This results in repetitive boilerplate code scattered throughout = the codebase. A new syntax, `(ClassName) $variable`, is proposed to streaml= ine this process by performing an instanceof check and throwing a `TypeErro= r` if the variable is not an instance of the specified class.
Proposal
***********

Int= roduce a new type guard syntax for classes:

```php=
(Foo) $variable;
```

This syn= tax will internally perform the following operations:

<= div>1. Check if `$variable` is an instance of `Foo`.
2. If the ch= eck fails, throw a `TypeError` with a message indicating the expected and a= ctual types.

Example:
Consider the follo= wing class definition:

```php
class Foo = {
=C2=A0 =C2=A0// class definition
}
```

To ensure a variable is an instance of `Foo`, instead = of writing:

```php
if (!$variable instan= ceof Foo) {
=C2=A0 =C2=A0 throw new TypeError('Expected insta= nce of Foo, got ' . gettype($variable));
}
```

Developers can use the new type guard syntax:

```php
(Foo) $variable;
```
=
Backward Compatibility
***************************=

This feature introduces new syntax and does n= ot affect existing code. It is fully backward-compatible, as it does not mo= dify or deprecate any existing functionality.
=
Since this throws, I'm struggling to understand how this= would replace any usages of `instanceof` other than `if (!($var instanceof= Foo)) throw new TypeError();`

Also, this is not a= n RFC with a page I can look at OR something in a vote, so the subject line= is a bit of a lie.

But now that I've gotten m= y grumps out of the way, better class specific syntax is in general somethi= ng that I think is positive and worth exploring.

J= ordan
--000000000000730bc506189b332d--