Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:104725 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 27954 invoked from network); 14 Mar 2019 22:51:25 -0000 Received: from unknown (HELO NAM04-SN1-obe.outbound.protection.outlook.com) (40.92.11.104) by pb1.pair.com with SMTP; 14 Mar 2019 22:51:25 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=giKgiAyMAAMv4Wx3FW9YDvJc5NPKLmF9yffJXCDqt3s=; b=StoYT0FOhCdDwdCX548EwahPwRzz8VPhs+2pEheAiop7b1RWqSyBE5OMF6Y1HnOz9fNbPO47/arU9g2fZLN0kw9YtWILADHvvfeBuPlT8Ft9rIwBGVyAomJoYnWtgK3XCVPt8eIwNdLlfyb+ArfwuoCADt2RXGoSxigXwj33qkrf9llXHTnJK8F9O70MbhPEShT+j6TqmabPEPQRe4Z/KxSiuMZ2G2J4OkXwGWgqU4b9T6wPktVK6gD5YEE6ASHomCzhQrk5CJj2nWxn/V8JtsTSZvmdSOvOUPTRQZAXrDUKrnUkcICFfCLBbywv1ghQlqeUDv6bknml24UmDTlgSA== Received: from CO1NAM04FT043.eop-NAM04.prod.protection.outlook.com (10.152.90.54) by CO1NAM04HT012.eop-NAM04.prod.protection.outlook.com (10.152.90.132) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.1709.13; Thu, 14 Mar 2019 19:41:51 +0000 Received: from DM5PR06MB2857.namprd06.prod.outlook.com (10.152.90.53) by CO1NAM04FT043.mail.protection.outlook.com (10.152.91.173) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.1709.13 via Frontend Transport; Thu, 14 Mar 2019 19:41:51 +0000 Received: from DM5PR06MB2857.namprd06.prod.outlook.com ([fe80::2580:dff:c36e:ea31]) by DM5PR06MB2857.namprd06.prod.outlook.com ([fe80::2580:dff:c36e:ea31%6]) with mapi id 15.20.1709.011; Thu, 14 Mar 2019 19:41:51 +0000 To: Nikita Popov , PHP internals Thread-Topic: [PHP-DEV] Re: [RFC] Arrow functions / short closures Thread-Index: AQHU2nxyT92G5anxlkG9179pVR0E5aYLhuNt Date: Thu, 14 Mar 2019 19:41:51 +0000 Message-ID: References: , In-Reply-To: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-incomingtopheadermarker: OriginalChecksum:F5ADB08ABE5EB1A8B05969C9EE8FB3C36770357BAAB2EB9095D5C7439DDD1380;UpperCasedChecksum:2D03DF7417017609AB721B704A71282358C87DE5780EE77952C6515D3E197A4D;SizeAsReceived:6934;Count:44 x-ms-exchange-messagesentrepresentingtype: 1 x-tmn: [TONhnaxcP/3ESaZIKAERemNOh+W+3tpR] x-ms-publictraffictype: Email x-incomingheadercount: 44 x-eopattributedmessage: 0 x-microsoft-antispam: BCL:0;PCL:0;RULEID:(2390118)(7020095)(20181119110)(201702061078)(5061506573)(5061507331)(1603103135)(2017031320274)(2017031322404)(2017031324274)(2017031323274)(1601125500)(1603101475)(1701031045);SRVR:CO1NAM04HT012; x-ms-traffictypediagnostic: CO1NAM04HT012: x-microsoft-antispam-message-info: a+c10P7D3MQ687m2ADwTYViGrpMQd1LC8l4iD7Z73mWGzoQHzj3svSMyzAMM6h2q Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-Network-Message-Id: ea761f83-c8e0-422d-5b07-08d6a8b51ae0 X-MS-Exchange-CrossTenant-rms-persistedconsumerorg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-originalarrivaltime: 14 Mar 2019 19:41:51.2471 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Internet X-MS-Exchange-CrossTenant-id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-Transport-CrossTenantHeadersStamped: CO1NAM04HT012 Subject: Re: [PHP-DEV] Re: [RFC] Arrow functions / short closures From: theodorejb@outlook.com (Theodore Brown) On Thu, March 14, 2019 10:41 AM Nikita Popov wrote:= =0A= =0A= > On Wed, Mar 13, 2019 at 4:56 PM Nikita Popov wrote= :=0A= > =0A= > > Hi internals,=0A= > >=0A= > > Motivated by the recent list comprehensions RFC, I think it's time we t= ook=0A= > > another look at short closures:=0A= > >=0A= > > https://wiki.php.net/rfc/arrow_functions_v2=0A= > =0A= > As a small update, I've implemented a proof of concept that uses the ($x)= =0A= > =3D=3D> $x * $multiplier syntax (or $x =3D=3D> $x * $multiplier for short= ) in=0A= > https://github.com/php/php-src/pull/3945. As mentioned in the RFC, this= =0A= > requires scanahead in the lexer.=0A= > =0A= > This syntax is in principle still on the table, though personally I prefe= r=0A= > fn($x, $y) =3D> $x * $y over ($x, $y) =3D=3D> $x * $y. The main redeeming= quality=0A= > of =3D=3D> is that it supports the paren-less variant $x =3D=3D> $x. Taki= ng into=0A= > account the lexer hackery it requires (and which will also be required in= =0A= > any 3rd party tooling), this would not be my preferred choice.=0A= >=0A= =0A= I agree that the nicest thing about this syntax is the ability to save=0A= an additional 3 characters of boilerplate for the common use case of=0A= single-parameter arrow functions. However, I'm also not a fan of adding=0A= complex code hacks to make the syntax work.=0A= =0A= One alternative that doesn't seem to have had much discussion on list=0A= is the `\($x) =3D> $x * $y` lambda syntax. This would also allow parenthese= s=0A= to be omitted for single parameters, making it just as terse as the =3D=3D>= =0A= syntax without the need for any lexer hackery.=0A= =0A= Here's how the examples from the RFC would look:=0A= =0A= ```php=0A= function array_values_from_keys($arr, $keys) {=0A= return array_map(\$x =3D> $arr[$x], $keys);=0A= }=0A= =0A= =0A= $extended =3D \$c =3D> $callable($factory($c), $c);=0A= =0A= =0A= $this->existingSchemaPaths =3D array_filter($paths, \$v =3D> in_array($v, $= names));=0A= =0A= =0A= function complement(callable $f) {=0A= return \(...$args) =3D> !$f(...$args);=0A= }=0A= =0A= =0A= $result =3D Collection::from([1, 2])=0A= ->map(\$v =3D> $v * 2)=0A= ->reduce(\($tmp, $v) =3D> $tmp + $v, 0);=0A= ```=0A= =0A= One argument against this shorter syntax is that it wouldn't be as=0A= easy to google as `fn`. However, long term I think everyone would=0A= still get used to it, and I'm personally willing to add an answer=0A= to the top Stack Overflow search result for "php backslash keyword".=0A= =0A= The backslash syntax has precedent from Haskell, and also wouldn't=0A= introduce any BC break (who knows how many private codebases might=0A= already have functions named `fn`).=0A=