Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:118410 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 34630 invoked from network); 11 Aug 2022 07:01:16 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 11 Aug 2022 07:01:16 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 0EE0A18053F for ; Thu, 11 Aug 2022 02:03:16 -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.0 required=5.0 tests=BAYES_20,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE, SPF_PASS,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-ed1-f53.google.com (mail-ed1-f53.google.com [209.85.208.53]) (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 ; Thu, 11 Aug 2022 02:03:15 -0700 (PDT) Received: by mail-ed1-f53.google.com with SMTP id z22so22111697edd.6 for ; Thu, 11 Aug 2022 02:03:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc; bh=F3k4nR9XB2X/uBux/87bjWz1toDOAF2Owtz8R0wJha0=; b=ArWuRzcYnZblHXYHa0Qug/xa54Fxrwt5jkjkGczs848hpNfmLsXxgpqHNlm6XimMm7 bcSJcu6KMoiuvuz3eA716tspwen0YKtxW1ThdLmkv0Ow2DWN8EpfJkhUPPyv2quI074L wUdygmm2K2vFliVq7lrUSTF6kVdJhAewFlLk7NsY/2ax5rSISN8pek6TpE2FiEo1gm6e REJ+NLcWc9cxXRFShXDIO4iyMyD3EU7QSwRrJb5He5l5U25wizF4Ffumqyrm1AFQ+4o4 n8jlZaXReBUnQAWB05jgVkaIHewnZ0UHPEJ7ZmTnBvdkfi7MUs8nNV/t5osSberhst0U 692Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-message-state:from:to:cc; bh=F3k4nR9XB2X/uBux/87bjWz1toDOAF2Owtz8R0wJha0=; b=SxcAvCtwk3taYaz7fuMOs1TgD+3skFc/GLjL0EcDILoR58gO8xfQwgSqlIylCtp4Xl e++kE/ALL/Ve8gHxu7d9vRl+TWtJmgX4GtQdXZe+B9DYQ0kovIsfpC4bqipD6lsvNE6a ChNEzfVFf+qkG/Rzb3QKMNG2xaRPQV/55NzcLuAs1WwwWSYVsnrARHyYDZW7KUjgwCwb 1g1/fUbdxnqjqjTNfJH0+WL1UrnUkcun3ifXi/qxn3Bu8vHOZ1qMon4BopsK9YlesHrO YHYcnNiezJmcJ5vOmGm7+eccIHYTsdQJm6G9kcTzljJBozvX+LVYPjMWlnNOfRkpmdl9 SHCA== X-Gm-Message-State: ACgBeo1N3s7LIlsABZamWNFlZ26PeWRWP7z3z4jrFyP9XofxCoejhd6S G74WmKkkFHiJpRAoMhp1+K7zK6+sQX8WSWIh X-Google-Smtp-Source: AA6agR5YRydwkS2SzWYkJmEWtEyCLcQaT+3DRtA7TRDorTJUTLzEp9hfVmc5mt+bnOR8KL4H92X0Yg== X-Received: by 2002:aa7:de8c:0:b0:440:3516:1813 with SMTP id j12-20020aa7de8c000000b0044035161813mr26146443edv.260.1660208594071; Thu, 11 Aug 2022 02:03:14 -0700 (PDT) Received: from smtpclient.apple ([93.175.202.231]) by smtp.gmail.com with ESMTPSA id r25-20020a056402019900b0043d7923af6esm8752763edv.95.2022.08.11.02.03.12 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 11 Aug 2022 02:03:13 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3696.100.31\)) In-Reply-To: <62f48769.250a0220.37387.53b5SMTPIN_ADDED_MISSING@mx.google.com> Date: Thu, 11 Aug 2022 12:03:11 +0300 Cc: internals@lists.php.net Content-Transfer-Encoding: quoted-printable Message-ID: <01D4987C-FD37-4BD2-A1AC-DD10F808F248@gmail.com> References: <62f48769.250a0220.37387.53b5SMTPIN_ADDED_MISSING@mx.google.com> To: Paul Crovella X-Mailer: Apple Mail (2.3696.100.31) Subject: Re: [PHP-DEV] [Concept] Extension methods From: autaut03@gmail.com (Alex Wells) > On 11 Aug 2022, at 07:36, Paul Crovella = wrote: >=20 > This isn't impossible. There's nothing stopping an IDE from seeing = `$collection->` and suggesting/completing that with `map($collection, ` = - they just don't right now. Offhand I don't see why this would be more = difficult for them to implement than support for extension methods. As a = bonus it'd work with existing functions. What=E2=80=99s stopping them is that those functions aren=E2=80=99t = methods. It=E2=80=99d be strange for `$collection->m` to auto-complete = to `map($collection, `. Even more strange would be the case where = you=E2=80=99re trying to auto-complete on a big multiline expression.=20 Besides, I believe working with existing functions is more of a problem = than a bonus - because of the naming. You=E2=80=99d have to clutter your = code with `array_map` calls, even though it could easily be shortened to = `map`. > On 11 Aug 2022, at 07:36, Paul Crovella = wrote: >=20 > In short every ugly example has a fine, easy-to-read way of writing it = now in existing code. A useful readability comparison would pit the new = feature against the best looking version of example code you can come up = with rather than the worst. Without that you're not really demonstrating = improvement. That=E2=80=99s just a concept. I=E2=80=99d love to bring a lot more = examples in an RFC if there=E2=80=99s more positive than negative = feedback. Again, I=E2=80=99m more looking for feedback than trying to = convince someone, but I=E2=80=99ll showcase a couple real comparisons = for some context: Compere these (copied from real world project/public composer packages): ```php $className =3D Str::studly(implode('_', array_slice(explode('_', = $file->getName()), 4))); // vs $className =3D = $file->getName()->explode(=E2=80=98_=E2=80=99)->slice(4)->implode(=E2=80=98= _=E2=80=99)->studly(); array_map(fn (string $item) =3D> trim(mb_strtolower(strip_tags($item))), = trans('contacts::tenants.gender_types=E2=80=99)); // vs trans('contacts::tenants.gender_types=E2=80=99)->map(fn (string $item) = =3D> $item->stripTags()->lower()->trim()); array_merge($oldResponse, array_map(fn (Response $v) =3D> = Arr::except($v->toArray(), 'message'), $newResponse)) // vs $oldResponse->merge($newResponse->map(fn (Response $v) =3D> = $v->toArray()->except(=E2=80=98message'))) ``` All these changes lead to a lower cognitive load, because: - you aren=E2=80=99t dealing with lots of nested parenthesis - you aren=E2=80=99t trying to read expressions =E2=80=9Cinside-out=E2=80= =9D - you aren=E2=80=99t messing with not-so-useful prefixes like `Arr::`, = `array`, `str` You could argue the problem is that all of these are single-liners, so = here are the same examples, but multiline formatted: ```php $className =3D Str::studly( implode( '_=E2=80=99,=20 array_slice( explode( '_=E2=80=99, $file->getName() ),=20 4 ) ) ); // vs $className =3D $file->getName() ->explode(=E2=80=98_=E2=80=99) ->slice(4) ->implode(=E2=80=98_=E2=80=99) ->studly(); array_map( fn (string $item) =3D> trim( mb_strtolower( strip_tags($item) ) ), trans('contacts::tenants.gender_types=E2=80=99) ); // vs trans('contacts::tenants.gender_types=E2=80=99)->map( fn (string $item) =3D> $item ->stripTags() ->lower() ->trim() ); array_merge( $oldResponse,=20 array_map( fn (Response $v) =3D> Arr::except($v->toArray(), 'message'),=20 $newResponse ) ) // vs $oldResponse->merge( $newResponse->map( fn (Response $v) =3D> $v ->toArray() ->except(=E2=80=98message=E2=80=99) ) ) ``` I believe the difference is quite obvious.=20 Not an argument, but one more thing to consider: if current state of = things in PHP was =E2=80=9Cgood enough=E2=80=9D, why do library authors = come up with those magic method trait solutions, and why we=E2=80=99re = seeing types like `Collection` being used instead of `array` and = `Stringable` (laravel) instead of `string`, when you could instead just = introduce a bunch of functions? > On 11 Aug 2022, at 07:36, Paul Crovella = wrote: >=20 > An extension method would shift a single argument from inside = parentheses to the left of the function name. It just moves it. I don't = see any impact here. It doesn=E2=80=99t move it, it removes it. You=E2=80=99re technically = not passing that value as an argument anymore anywhere in your code, = your value becomes the implicit expression you=E2=80=99re working with. = That becomes even more obvious when multiple method calls are chained = and don=E2=80=99t need to pass it into every function call. > On 11 Aug 2022, at 07:36, Paul Crovella = wrote: >=20 > This is essentially making `->` the pipe operator with extra steps = (`extension`/`use extension`) and less utility (not working on existing = functions.) Well, pipe operator is another option, but it=E2=80=99s got it=E2=80=99s = downsides compared to extension methods: - it's less versatile: extension methods are required to specify a type = they=E2=80=99re extending, meaning they are methods, not functions. = Hence, two different `map` method extensions can be imported in a single = file (given they=E2=80=99re for different types - say one for = `Collection`, the other for `array`), unlike regular functions. I = believe it=E2=80=99s common place to use both `Collection::map` and = `array::map` in a single file, but that wouldn=E2=80=99t be possible or = would require aliasing - it=E2=80=99s uglier: since it just uses functions, to avoid clashes = between same method names, prefixes would be required