Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:109775 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 93286 invoked from network); 22 Apr 2020 16:30:24 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 22 Apr 2020 16:30:24 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 06B341804B7 for ; Wed, 22 Apr 2020 08:02:06 -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=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_PASS 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-ot1-f41.google.com (mail-ot1-f41.google.com [209.85.210.41]) (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, 22 Apr 2020 08:02:05 -0700 (PDT) Received: by mail-ot1-f41.google.com with SMTP id z17so2262721oto.4 for ; Wed, 22 Apr 2020 08:02:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=AIbvBJsNCjUDRGQIz0xqyD9xgWM1sen1Btwpqp2X16g=; b=Xm4taoGfgTVlrVVoq8nnfPoTnqqS/TTHWTkRSBpoScy1Ssom41RGkB2Pgm8+JDxMlj xmZPXokvcR8DEvmybHB2J5+FYEyQmoSe4lcO0Xe+crIiTwkQgxlbxYBEesydKpdoUNYD 4fpLV9Ydzkqh3m5wcB9ayf18wkLmN0ZQ7/yW1VgO1ocwzNj1PI66yjhjOVXyrAPXrKkl oqBGgev+anD2gYwnD3MCIXKF/PjdJh0jJXa92HKoIGEfOxSBmdPQHNIXaPlWaNdnEBsb xKuLocbSDJnuhCJXpLKMlwoioP92W9JCuirLz/33ESutB1lhxpElKLsrS4hPUDdxTUZV DbLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=AIbvBJsNCjUDRGQIz0xqyD9xgWM1sen1Btwpqp2X16g=; b=bvqnoXkLk9Wzx6DqD56jlFrWI2+/IMDjHZRjxDqo/xvffCsYlVr/SYqlZj7LhZvgH9 GWVd8is2d1JHs0jQbKzGXPBA7851ggv5fKulorx7LZBes3/rxUSl5elIh5WuoEBrOvTw 4ovsbrn23sb0ZXP2bQQqyDxxn8W5eMTDZuRxu3UcdfnMzT0aufXOd8zh99fge3xyHvih xEduNbXP09zQKJla2MypG+uGPlnqoK8QjmFZ8W8ceKlnvbOVqgJ+gA+1sQ30/4zZx2an IBFLcXuShsTXMA/fdvwpRho6/ZTpGBXO2/a8l2PC8fqxztLK+qKzP3OtYVpRocQ4TxIU NHxA== X-Gm-Message-State: AGi0Pub2BWDmdufMmOLC4tafGZN+haLYKV0aNDor6YfNNqcCdsag5X7B Fl3sgngM9z4gOQ05fQZ23+CTB1+Rm/jhpRm8aT0= X-Google-Smtp-Source: APiQypId4B9RWmYnQxybaJK7QwT+pBAWVFiGYlrfEwHOZcn6oQ5zpDyj5ABrcnSQqLIvQVcFhQ3HzjG54fWPJpDtdEQ= X-Received: by 2002:a05:6830:114e:: with SMTP id x14mr17198837otq.214.1587567723658; Wed, 22 Apr 2020 08:02:03 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Wed, 22 Apr 2020 17:01:51 +0200 Message-ID: To: Dan Ackroyd Cc: moliata , "internals@lists.php.net" Content-Type: multipart/alternative; boundary="00000000000039c93905a3e26af2" Subject: Re: [PHP-DEV] Typed callable properties From: michal.brzuchalski@gmail.com (=?UTF-8?Q?Micha=C5=82_Brzuchalski?=) --00000000000039c93905a3e26af2 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi all, =C5=9Br., 22 kwi 2020 o 16:29 Dan Ackroyd napisa= =C5=82(a): > On Tue, 21 Apr 2020 at 14:08, moliata wrote: > > > > > I wanted to ignite a friendly discussion whether > > PHP should allow typed callable properties. > > IMO no. > > 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. > Trying to fix the problems with callables would be a huge amount of > work, and not actually give that much benefit. Even just documenting > the problems with callables* is a non-trivial amount of work, and I > suspect there are many horrors lurking with the SPL code related to > them. > > > I believe we should look into... > > I'm pretty sure that choosing a different problem to solve that: > > * would be easier to solve. > * provide more benefit in the long term. > * not require breaking a lot of userland + internal code immediately, > but instead allow for migration over a longer period. > I was pinged by Dan with typedef topic nearly 3 weeks ago and then started thinking of the way to define callable types with some initial implementation. I chose pattern known from other languages like C# where there are types known as delegates. And so far got to the last line of snippet below where I have to figure out some clever type checking with closure: delegate Reducer (?int $sum, int $item =3D 0): int; class Foo implements Reducer { public function __invoke(?int $sum, int $item =3D 0): int { return ($sum ?? 0) + $item; } } function reduce(Reducer $reducer) { var_dump($reducer(0, 5)); } reduce(new Foo()); reduce(fn(?int $sum, int $item =3D 0): int =3D> 8); The delegate declaration resolves to an interface with __invoke method which therefore can be easily checked when the invokable object passed and that information can be easily cached. Probably it can be cached also for closures and functions but didn't get so far with the implementation yet. I was also asked why not a general use typedef which can be used to alias any kind of type not only a callable. But the reason why I chose delegates was that IMO typedef is more like an aliasing mechanism, which means all that is possible to be aliased should also be possible to be unaliased and pasted in all type constraints used in function/method parameters as well as class properties. Meaning if we allow: typedef reducer =3D callable(?int $sum, int $item =3D = 0): int; We should also allow: function(callable(?int $sum, int $item =3D 0): int $reducer) {} Which IMO looks too verbose and that's why I think a delegate might be a good idea as a way to provide callable types checking. Any thoughts are highly appreciated! Cheers, Micha=C5=82 Brzuchalski --00000000000039c93905a3e26af2--