Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:119406 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 78843 invoked from network); 23 Jan 2023 18:32:27 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 23 Jan 2023 18:32:27 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 57C341804F2 for ; Mon, 23 Jan 2023 10:32:26 -0800 (PST) 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.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-ej1-f51.google.com (mail-ej1-f51.google.com [209.85.218.51]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Mon, 23 Jan 2023 10:32:25 -0800 (PST) Received: by mail-ej1-f51.google.com with SMTP id az20so33010116ejc.1 for ; Mon, 23 Jan 2023 10:32:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=basereality-com.20210112.gappssmtp.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=DCmHWTpDMUw+gzplljbfzSwFWlnYrf4wVhYZ6qKZW9g=; b=n4yY4c1DgaIlshvasCytvfuUQcKS5Wp0uroDLlfdPZwH55Lr/CDqltvV0QA+KzPfmT 3gA0kMrGtmy/PteUh6ERPKofY/dB27VzQ3wGCx2Yn/9nN0sehk9M4k7CDrKXz++7v5rg SvABNke02sI2X7d3u9SceIb420Pr7obVK8l7hmCTTnxzQB3VdVy/ZYDRgqoMOYOR7f3Q kCfaylEPaxz4jO/5VriDfMH7dM3gQBPlgOKg1rBI8YfpFRXf1yjlrStZR1JK/h3QdJa1 /xLcqKAIFcbiJfvbsPrunDlwZFzEIP6FLjPAHfk4/NaWJMLzYu98E/nx4rHcy5rZ26tq p6vg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=DCmHWTpDMUw+gzplljbfzSwFWlnYrf4wVhYZ6qKZW9g=; b=2WJWh/7v2KPerURm4o7WGH2b4Mx5OPvTYZJmOKkj8VMuLmwicgXz+IK13Hu9aMGvTp YcVbBzv14a1Vk9Vyr+lkV6fJAX2jPJz4h6gR+ME43sMvulHe2SVRqjRO3D+GR1lRprlf HOzCEuK9S2MAgqlzdWasAakJHSfw2eMxgjCkhyd9Gk3fsa0+wati9oiBQEceeFy/+yFD 2+q6xZXd6f3tfAEGW+3UEV2G1SE5LXf0hnrlTclaiWHqrt6mqiG6klg0ANXOoOOpy7Ff G/ovkTRZuoE5QI+gx7/PDt2ojBKW0IGI0m/CdkxIn6VQulOERNN19KuHyL4319hsvfMK uVDA== X-Gm-Message-State: AFqh2kodFY9jvdk3lbEPBjCGOMHBOTMrKjk5fZlDtZnJ+grgQDFOb+rF UZNxtpxm2GAbv7e6h6fKDtguh2XgPDrzRm8PcENEl2e4LZuvEGJD X-Google-Smtp-Source: AMrXdXsBcVQjv4kG+989l7SAEckRTiMH2fk5vKDnVrMTJ+OESoLjhngBX0hIpHi/PDl+xSzIyn8Rh9AYWibdjglwmcM= X-Received: by 2002:a17:906:1d4a:b0:7c0:d125:1fe6 with SMTP id o10-20020a1709061d4a00b007c0d1251fe6mr2931579ejh.514.1674498743541; Mon, 23 Jan 2023 10:32:23 -0800 (PST) MIME-Version: 1.0 References: <8f530a4a-bf48-4259-877e-16ebf8082936@app.fastmail.com> In-Reply-To: <8f530a4a-bf48-4259-877e-16ebf8082936@app.fastmail.com> Date: Mon, 23 Jan 2023 18:32:12 +0000 Message-ID: To: Ollie Read Cc: =?UTF-8?Q?Bj=C3=B6rn_Larsson?= Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] Introduce the abiltiy to use the first-call-callable syntax on non-static methods, statically From: Danack@basereality.com (Dan Ackroyd) On Sun, 22 Jan 2023 at 17:45, Ollie Read wrote: > > Hello all, Hi Ollie, > I've created a feature request issue on GitHub (here: https://github.com/php/php-src/issues/10414), but I have been advised that it's best to post here. > ... > I think we could delay the error until the closure was called. That sounds like a complete non-starter. Adding something to the language that produces a closure that can't be called would be an instant new entry for PHPSadness. > Whereas it would be much nicer to have the following: > > ``` > $collection->filter(Str::empty(...)); > ``` > > In this situation, the collection library would be responsible for binding the > closure to the value it is iterating. How would the collection library know it was safe to bind the closure to each value it was iterating over? It sounds really type unsafe. > What I would like to introduce/suggest, is the ability to create a closure > from a method using the first-class-callable syntax... > > The more I think about it, the more I think this may require a new ... I think people should say clearly what the problem they are trying to solve first, before suggesting solutions. I think there's at least a couple of problems that can be thought about: i. Although the first-class-callable syntax allowed avoiding string based programming for most callables, it doesn't support referring to some things that you would want to call, including both instance methods, and constructors*. ii. Representing an instance method requires at least two pieces of info; which class it belongs to (which can be used to find the constructor) and the parameters of the method itself. That doesn't naturally fit into a closure. iii. The appropriate syntax for referencing a class instance method without it being string based isn't obvious. Or at least it isn't obvious to me. iv. Writing code that for callback methods is longer than it could be, e.g. as Larry Garfield wrote: > So what you're really looking for is a shorter way to write this: > > foo(fn(Str $s) => $s->beep()); At the risk of suggesting an abomination, given a class of: class Zoq { public function __construct(private Fot $fot) {} public function Pik(string $zebranky): Frungy {...} } If it was possible to generate callable for the constructor with: $fnConstructor = Closure::fromClassConstructor(Zoq::class); // signature of $fnConstructor is the same as `function(Fot $fot): Zoq` Or for individual methods: $fnMethod = Closure::fromClassMethod(Zoq::class, 'Pik'); // signature of $fnMethod is the same as `function(Zoq $zoq, string $zebranky): Frungy` That sort of looks like a solution to most of the problems I think exist. For your particular problem, I believe that would allow: $fnMethod = Closure::fromClassMethod(Str::class, 'empty'); $collection->filter($fnMethod); Which isn't shorter, but at least allows passing the callables around with the type inspectable. Though it might be nicer if PHP had the ability to definte function signatures types. That sort of solution obviously doesn't address the problem of having to refer to the class method as a string, but as I said, I don't have a suggestion for that. Well, other than to use a different syntax.** cheers Dan Ack * constructors having their own terrible history. ** https://news-web.php.net/php.internals/114542