Req #80208 [Opn->Sus]: Scalar pseudo-type (like Iterable)

From: Date: Fri, 09 Oct 2020 12:01:49 +0000
Subject: Req #80208 [Opn->Sus]: Scalar pseudo-type (like Iterable)
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-229485@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=80208&edit=1 ID: 80208 Updated by: cmb@php.net Reported by: divinity76 at gmail dot com Summary: Scalar pseudo-type (like Iterable) -Status: Open +Status: Suspended Type: Feature/Change Request Package: Unknown/Other Function PHP Version: Next Minor Version Block user comment: N Private report: N New Comment: Yes, that would need an RFC. Feel free to pursue. For the time being, I'm suspending this ticket. Previous Comments: ------------------------------------------------------------------------ [2020-10-09 11:36:19] kocsismate@php.net I have a fairly up-to-date PR exactly for this which I don't plan to pursue: https://github.com/php/php-src/pull/5483. Please feel free to start an RFC by using this implementation if you would like this functionality in PHP. ------------------------------------------------------------------------ [2020-10-09 11:26:23] divinity76 at gmail dot com ... i should have googled more, apparently it's already an "RFC under discussion", https://wiki.php.net/rfc/scalar-pseudo-type ------------------------------------------------------------------------ [2020-10-09 11:15:24] divinity76 at gmail dot com Description: ------------ i suggest a scalar pseudo-type, much like Iterable, today if one wants to accept scalar arguments, one has to write something like: <?php function f($sc){ if(!is_string($sc) && !is_int($sc) && !is_float($sc) && !is_bool($sc)){ throw new \InvalidArgumentException('argument #1 ($sc) must be a scalar type'); } } class C{ public $sc; function set_sc($sc){ if(!is_string($sc) && !is_int($sc) && !is_float($sc) && !is_bool($sc)){ throw new \InvalidArgumentException('argument #1 ($sc) must be a scalar type'); } $this->sc = $sc; } } ?> luckily it gets easier in PHP8, <?php function f(string|int|float|bool $sc){ } class C{ public string|int|float|bool $sc } ?> even so, if there was a Scalar pseudo-type, i would use it. Test script: --------------- <?php declare(strict_types=1); function fun(Scalar $sc){ var_dump($sc); } fun(1); Expected result: ---------------- int(1) Actual result: -------------- Fatal error: Uncaught TypeError: fun(): Argument #1 ($sc) must be of type Scalar, int given, called in /in/PDjNq on line 6 and defined in /in/PDjNq:3 Stack trace: #0 /in/PDjNq(6): fun(1) #1 {main} thrown in /in/PDjNq on line 3 Process exited with code 255. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=80208&edit=1

« previous php.bugs (#229485) next »