Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:109782 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 50605 invoked from network); 22 Apr 2020 19:02:00 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 22 Apr 2020 19:02:00 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 2443B1804B7 for ; Wed, 22 Apr 2020 10:33:43 -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=-1.3 required=5.0 tests=BAYES_00,BODY_8BITS, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_LOW,SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS62371 185.70.40.0/24 X-Spam-Virus: No X-Envelope-From: Received: from mail4.protonmail.ch (mail4.protonmail.ch [185.70.40.27]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 22 Apr 2020 10:33:41 -0700 (PDT) Date: Wed, 22 Apr 2020 17:33:29 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail; t=1587576819; bh=eD62XSVoGz1o67NF3L4b0VnLP7mJ6u0O8mbYxaXBd5I=; h=Date:To:From:Cc:Reply-To:Subject:In-Reply-To:References:From; b=IA8aLvgXG025DLOUOnnzj5nnEbUV4PWuEF109nFWhKLpgGaqjd43rZA3ktMhuppVA LNq/LP728BQ415/GQYv43rNulMMJwyMW0os06OsTsZfqMl3sNNHglJavKOFCiE58Og myb5+WFGlwRfq+pjQEPbV+ONQJo4wXPZ+hDT8Low= To: Rowan Tommins Cc: "internals@lists.php.net" Reply-To: moliata Message-ID: In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Typed callable properties From: moliata@protonmail.com (moliata) Hello, thank you for the comment! Yes, indeed I was raising a discussion on whethe= r we should allow making typed callable properties for improved language's consistency (given that parameter/return type hints can be set to callable)= . As one of the solutions, I mentioned that we could just ignore the scope/context-dependent problems for the time being. In regards to your comment on `Closure::fromCallable()`, I do also agree th= at it is quite verbose and a potential performance hit and as such, this isn't= the way to go. As an example, let's imagine a framework that allows registering routes to callables. If we wanted to completely type-hint the entire framew= ork, we would have to make typed Closure properties and in the `addRoute()` func= tion used `Closure::fromCallable()` to convert every single callable into a clos= ure object. So, huge applications with thousands of routes would be potentially affected by this additional overhead. Best regards, Benas Seliuginas =E2=80=90=E2=80=90=E2=80=90=E2=80=90=E2=80=90=E2=80=90=E2=80=90 Original Me= ssage =E2=80=90=E2=80=90=E2=80=90=E2=80=90=E2=80=90=E2=80=90=E2=80=90 On Wednesday, April 22, 2020 7:30 PM, Rowan Tommins wrote: > On 22/04/2020 16:01, Micha=C5=82 Brzuchalski wrote: > > > I agree, a callable brings as much information as a resource type - you > > know the type > > but are unable to use it without additional information. > > While this is true, it's somewhat orthogonal to the question Benas > raised in this thread - namely, how to handle values that are callable > in one scope but not another. > > That is, is the following code valid (error-free) or not? > > delegate Reducer (?int $sum, int $item =3D 0): int; > class X { > =C2=A0=C2=A0=C2=A0 private static function myPrivateMethod(?int $sum, int= $item =3D 0): > int { > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return 0; > =C2=A0=C2=A0=C2=A0 } > =C2=A0=C2=A0=C2=A0 public static function runReducer(Reducer $r) { > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return $r(0, 0); > =C2=A0=C2=A0=C2=A0 } > } > echo X::bar(['X', 'myPrivateMethod']); > > The equivalent code with a type constraint of "callable" currently runs > fine, even though passing the same value to a different function would > fail due to scoping. Making the delegate-constrained version throw an > error for any private or protected method would just trade one > inconsistency for another. > > A more reasonable restriction, IMO, would be to say that delegates only > match Closure instances (and possibly other objects with a public > __invoke method), not the various string and array syntaxes that > "callable" currently allows. The case above would then need to bind the > private method into a closure that is callable everywhere, e.g. using > Closure::fromCallable([self::class, 'myPrivateMethod']); > > The obvious downside is that Closure::fromCallable is both verbose and a > potential performance hit. So it seems we're back again to wanting a new > syntax to succinctly and optimally generate such a closure. For > instance, in the discussion of ::func, one suggestion was > "$closure=3D{self::myPrivateMethod};" and more recently it was mentioned > that a variant of partial application is to not actually bind anything, > and write "$closure=3Dself::myPrivateMethod(?, ?);" > > Regards, > > -------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ------------------------------------------------ > > Rowan Tommins (n=C3=A9 Collins) > [IMSoP] > > ----------------------------------- > > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php