[php-src] Issue #11465: Flexible Return Type (The «Not Type» syntax in the function return)

From: Date: Fri, 16 Jun 2023 19:13:07 +0000
Subject: [php-src] Issue #11465: Flexible Return Type (The «Not Type» syntax in the function return)
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-244714@lists.php.net to get a copy of this message
Issue: https://github.com/php/php-src/issues/11465 Author: DevDasher ### Description ## I am a beginner in PHP. I don't know if this feature that I suggest below will be useful or not. But please leave this post open so that other users can see it, maybe they will come up with better ideas # Title: Flexible Return Type (The «Not Type») ## Introduction This RFC proposes a new flexible return type for PHP functions and methods that allows developers to specify what values the function or method must not return. This new return type will help developers write more expressive and flexible code. ## Motivation Now, we can set the return values we want in the functions. But in some scenarios we only need one or more specific types not to be returned. ## Proposal We propose adding a new return type, denoted by the syntax ~(type) where type can be any type that the return value must not match. This syntax indicates that the return value must not be of the specified type(s), but can be any other value. For example: ```php function test(string $value): ~(bool) { // can return any value except boolean. } class Test { public function test(string $value): ~(bool|false) { // can return any value except boolean or false. } } ``` In these examples, the test() function and method can return any value except for boolean, and boolean or false respectively. ## Examples Here are some additional examples of how the flexible return type can be used: ```php // must not return true function get_data(): ~(true) { $data = fetch_data_from_database(); if (!$data) { return 'Error: could not fetch data'; } return $data; } // must not return null or false function get_users(): ~(null|false) { $users = fetch_users_from_database(); if (!$users) { return 'Error: could not fetch users'; } return $users; } // must not return an object function get_string(): ~(object) { return 'this is a string'; } ``` In these examples, the flexible return type ensures that the functions and methods return only values that are not of the specified types. ## Backward Incompatible Changes This proposal does not introduce any backward incompatible changes. ## Proposed PHP Version This feature is proposed for inclusion in the next major version of PHP (PHP 9). ## Alternative Syntax As for the syntax improvement in ~(type), we suggest using not followed by the name of the type as the new syntax. This would make the code more readable and easier to understand. Here's an example of how this would look in the code: ```php // must not return true function get_data(): not true { $data = fetch_data_from_database(); if (!$data) { return 'Error: could not fetch data'; } return $data; } // must not return null or false function get_users(): not null | false { $users = fetch_users_from_database(); if (!$users) { return 'Error: could not fetch users'; } return $users; } // must not return an object function get_string(): not object { return 'this is a string'; } ``` The not type syntax conveys the same meaning as ~(type), but is more human-readable.

« previous php.bugs (#244714) next »