Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113988 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 1615 invoked from network); 7 Apr 2021 13:26:26 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 7 Apr 2021 13:26:26 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id B5D55180501 for ; Wed, 7 Apr 2021 06:25:40 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.6 required=5.0 tests=BAYES_00,BODY_8BITS, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS, SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-40137.protonmail.ch (mail-40137.protonmail.ch [185.70.40.137]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 7 Apr 2021 06:25:39 -0700 (PDT) Date: Wed, 07 Apr 2021 13:25:30 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail; t=1617801936; bh=DbPbCgcGve347lDYrNMxSkY+29fxsDBrDUq07oYFL48=; h=Date:To:From:Cc:Reply-To:Subject:In-Reply-To:References:From; b=udOYr4gdtb8wFZ21Luh8jVusfaJZf1vtoQ6ptAK8bOgM0yjjZ97K9+HfPPgUPEbq2 id+VB7GeGWVG6A7HjVd9zSXODgRcuFOEGgeiaV2HiPZYu69rXM/SckpTOJ92JLCMva UQrvWDjjSKdrUKfJzq+j4wqUmjJdjqgi4IVPf0y8= To: Hamza Ahmad Cc: "internals@lists.php.net" Reply-To: Saif Eddin Gmati Message-ID: In-Reply-To: <007001d72b9c$e3b3e870$ab1bb950$@gmail.com> References: <007001d72b9c$e3b3e870$ab1bb950$@gmail.com> MIME-Version: 1.0 Content-Type: multipart/signed; protocol="application/pgp-signature"; micalg=pgp-sha256; boundary="---------------------64ca9a3472989ba81f8bf4aff24c914a"; charset=utf-8 Subject: Re: [PHP-DEV] Empower Callables with Function Signature Hints From: azjezz@protonmail.com (Saif Eddin Gmati) -----------------------64ca9a3472989ba81f8bf4aff24c914a Content-Type: multipart/mixed;boundary=---------------------7695eb5bea672f54584d90c72e866a18 -----------------------7695eb5bea672f54584d90c72e866a18 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain;charset=utf-8 Hello Hamza, I think this is something that has been discussed here before. function types already exist in HackLang: https://docs.hhvm.com/hack/funct= ions/introduction#function-types, And, if we would follow the same syntax `(function(T, T): T)` or `(callabl= e(T, T): T)`, PHP needs to first support parentheses in type declaration ( grouping ), which currently it doesn't ( i.e: `function= baz((string|bar) $foo): void {}` becomes valid ). And just to note, function types are also supported by static analysis too= ls such as Psalm ( https://psalm.dev/docs/annotating_code/type_syntax/call= able_types/ ) and PHPStan ( https://phpstan.org/writing-php-code/phpdoc-ty= pes#callables ), so i suppose if PHP is to support typed callables, it sho= uld take inspiration from these tools. There's also an alternative syntax that was proposed back in 2016 for PHP = 7.1, but it seems like the RFC was abandoned: https://wiki.php.net/rfc/typ= esafe-callable Cheers, Saif. =E2=80=90=E2=80=90=E2=80=90=E2=80=90=E2=80=90=E2=80=90=E2=80=90 Original M= essage =E2=80=90=E2=80=90=E2=80=90=E2=80=90=E2=80=90=E2=80=90=E2=80=90 On Wednesday, April 7, 2021 11:58 AM, Hamza Ahmad wrote: > Hello Internals, > I want to request making possible to catch functions with wrong signatur= e > when passed as callable. In other words, when a function or a method is > passed as callable value, PHP will first confirm whether the function is= of > the write callable type that the function being executed requires. It wi= ll > help catch silent errors. > Currently, PHP does following: > = > 1. Confirms whether a callable is valid. > 2. Passes the callable value to a function, and the execution starts. > 3. If the passed function receives values which's type is different tha= n > function signature, PHP throws an error. > = > 4. If passed function returns nothing, the variable that wanted a value= from > the passed function receives null. > = > 5. As a result, the unexpected results emerge. > To resolve this issue, I propose to introduce a mechanism that allow= s to > hint callable signatures. To understand the spirit of this request, = look at > the similar functionality that Angel Script Project provides with. > = > 6. One defines a function definition. > 7. Use its name to hint the same signature. > 8. The compiler alerts about the incorrect signature before passing thi= s to > a function. > If I could do this in PHP, I would write like following: > `funcdef preg_replace_callback_hint(array $matches) : string;` > If I accidently pass a callable which's signature is different than = desired > function call, the error is caught earlier. > This message does not suggests syntax; it only points to a problem a= nd > suggests a solution. Any volunteers that would love to come up with = syntax > and implement this? > I am curious to see the feedback. > Regards > Hamza Ahmad > = > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > -----------------------7695eb5bea672f54584d90c72e866a18-- -----------------------64ca9a3472989ba81f8bf4aff24c914a Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: ProtonMail wsFzBAEBCAAGBQJgbbLJACEJELAOCkaz8cFXFiEEOazMpP0wDQTIQG6zsA4K RrPxwVepiA/8Df3Hel/BHoqI3TTcXdc18iHALP4BNLk/sxlbDcmO61plhBlo D7DjFVvHu6FreKF8/lsJmsMj8RDqjpngpL2z9Uy1F2ZZPz3PQ9ZkgAjwmZoG GVivK+oBNFmW2Pifo54jbWZ8WVbssLPHZGbPn0KfutgZxV6kamO7VbpvcJGg p1tQWFySUcn+MQG5/YqnH7kSu8gvZQFY28dmKO3svQvYBy60gXqLTWhDh+Mz 82mln4LfBeDM1xQCwOBayyaV8bdLyHmRYC3kDn1cEUnUUcmUal/X9JFMhyK2 YAmgL+6ofLS62rTL0+hbs60dN8njUccUWPCLj4Enw28wV68TuhBiRfFIg+y9 FRuSWWv5EzqDEVqq44j67G0L0JD5a7gL+6Zl86g6RrpWSmJ7TjWAs9e0ghej StyDGBS3J3iTpSCUUkSZcrYmhQTZ8piO+vp/tbyzgyxVS4A9UuQSkBeZpeJx s5ia32WzzPBQYqL/kWklWfTPE7mfqxoUyY7SX1F87ZuxH+JsBde4YNPlzLoD lJS1NuDAKFoWMCoCgDMdey5gTdm9IviRRzNUVqoYhhyQ9fXx/m16qHfbUU+i 8SzDFC4LqQvt69VzGKRxSKFak9WQZ1pNxh5hcGDKN54aF18CHrsCMnJ61wIW iyFUnjFataGrdonq7oMeCv9j0HC+qVrSxDI= =yubJ -----END PGP SIGNATURE----- -----------------------64ca9a3472989ba81f8bf4aff24c914a--