Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:114000 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 98357 invoked from network); 8 Apr 2021 10:55:58 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 8 Apr 2021 10:55:58 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 75F6F180544 for ; Thu, 8 Apr 2021 03:55:28 -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_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-vs1-f52.google.com (mail-vs1-f52.google.com [209.85.217.52]) (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 ; Thu, 8 Apr 2021 03:55:28 -0700 (PDT) Received: by mail-vs1-f52.google.com with SMTP id e16so318592vsu.0 for ; Thu, 08 Apr 2021 03:55:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=BxYO0Z0itYN/OOohOUjtkyXAlz5LgxvdzAz4HfADTEY=; b=L2lYPPWVj0h74tB8L/YwCa7ST5+8glYugszSY70b2PjuwP5GqsSaoawhHeJhCisqYp bFRoc7wVhdojEyYFSGfW8aksPlEa0VwICZYW24XIOnNic8n5u3MwY0XyZsz79r41rCdJ GreNq4762U3mhx/p6I6vXwxLvstzzdTWC7HVQfgk/Vi4UowV7snkqQalLUokOx4D/2dv Lb0+5cJZlHTZgPYMVXeoVPZSLQse+nryz/KoU/db6CQfCNvfVDPl44MvitXILLjBGYdc 77x0AXwMP75kVyIcYfARa36QZb4NFmofNW3og/3rXB74N0GmwKIpXSJiapCvRQaaYW7m AYHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=BxYO0Z0itYN/OOohOUjtkyXAlz5LgxvdzAz4HfADTEY=; b=bcrOJlmaB/xhmmNPjGrnNz+WypkpJeq8tvK/ld+VEH2qP3jZnQ2Ndy4j196HaWWSpS WWqzUBjj/MnP6mQ4kMRDEgk5Yy1GFxvOWzgDnYDFn2feM4QUBOwWitGISfklNk2oqFU4 lnEC84ZpbT90eJNXldowi0rP36Hp8TN2koVFOSMON8zEmbG39T9jyseEYqQk8T1PgE98 A+MlBheLVMawYr+VJt7+v4f4IjjD5RoC1KrvFCiKv4bD+2KvpRfhfDrtC9eBIwuQsOkG 1S4ZAymk0qRMARFp36ZszOM8C1+QPdMRqZTJnpHsQGvaEkDQZg8f61CVQjIo6SE+hcRk c5RQ== X-Gm-Message-State: AOAM533MnoXo9cb5yHl1509D5vHOFMFONwqbfdpmP8AFM8Yvjugt7nZL x4kyebYkf7+h0Tp6YyOeny80d9SnM9arOdRtvgc= X-Google-Smtp-Source: ABdhPJyfjJOhmcEaZI8glRBpy0yog4gyXc2Cxiy94SlpnwvmAFcglnB9tJhXnqMah0OQpS2IrUlmOAbB8XRSrJQdsFw= X-Received: by 2002:a67:2fcb:: with SMTP id v194mr5162774vsv.25.1617879325765; Thu, 08 Apr 2021 03:55:25 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:ab0:36d6:0:0:0:0:0 with HTTP; Thu, 8 Apr 2021 03:55:25 -0700 (PDT) In-Reply-To: References: <007001d72b9c$e3b3e870$ab1bb950$@gmail.com> Date: Thu, 8 Apr 2021 15:55:25 +0500 Message-ID: To: Saif Eddin Gmati Cc: "internals@lists.php.net" Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Empower Callables with Function Signature Hints From: office.hamzaahmad@gmail.com (Hamza Ahmad) Hello, I thanks Saif for adding to my argument and providing with the list of other languages/tools that support such a functionality. As Saif has mentioned about "typesafe callables" RFC that is now inactive, I want to also point out toward another rfc that was declined. It was Callable Prototype RFC that had sought for the similar functionality. https://wiki.php.net/rfc/callable-types Python also provides with a library ever since 3.5: https://docs.python.org/3/library/typing.html After looking at the prior RFCs, I feel like that it requires a fresh discussion. Because there are several syntax to choose from, a separate vote will only be held for this. Best Hamza Ahmad On 4/7/21, Saif Eddin Gmati wrote: > Hello Hamza, > > I think this is something that has been discussed here before. > > function types already exist in HackLang: > https://docs.hhvm.com/hack/functions/introduction#function-types, > > And, if we would follow the same syntax `(function(T, T): T)` or > `(callable(T, T): T)`, PHP needs to first support parentheses in > type declaration ( grouping ), which currently it doesn't ( i.e: `functio= n > baz((string|bar) $foo): void {}` becomes valid ). > > And just to note, function types are also supported by static analysis to= ols > such as Psalm ( > https://psalm.dev/docs/annotating_code/type_syntax/callable_types/ ) and > PHPStan ( https://phpstan.org/writing-php-code/phpdoc-types#callables ), = so > i suppose if PHP is to support typed callables, it should take inspiratio= n > 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/typesafe-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 = Message =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 >> will >> 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 >> and >> 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 >> > >