Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:118420 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 82124 invoked from network); 11 Aug 2022 16:57:17 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 11 Aug 2022 16:57:17 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id AC567180543 for ; Thu, 11 Aug 2022 11:59:23 -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.9 required=5.0 tests=BAYES_00,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-f45.google.com (mail-ed1-f45.google.com [209.85.208.45]) (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 11:59:23 -0700 (PDT) Received: by mail-ed1-f45.google.com with SMTP id z2so24124409edc.1 for ; Thu, 11 Aug 2022 11:59:23 -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=f/tAxCbue32rD8pZ2dvtUAteuBd5+zczV3n3RQkEgfM=; b=EcmyVMSQ155ibOKBDrLnruMpdYRW3G+GFoPquKIgrhgU2l4HxGDr90u7Jr/NttA4nL OKpnuJRZ3+iy5LxrvntWbqx0xHt1h8PvsxyuNER79u+bbOxpi+DuKIXIz+UNFDRkrNpa AdpfUZ6s12yPX/ammn3glN/pLYw1zCyZErcxt0FdooPDwsNeq1fn7tqEAaQORtiR/nQW BkMYin8QRXPA88SUC3Q6+oa3wA0JUtP7MPEfqwOt/AWRhq5hOfmPzZkFIVML7LVqnbAG 2uOEO9F/qj3utBEiYddxN8mawC9qwY/JrgyjsWfXB0cx6ZRyMOHhyBuoDcffwy+G1Ura hvnw== 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=f/tAxCbue32rD8pZ2dvtUAteuBd5+zczV3n3RQkEgfM=; b=rUnVE1BZ7pt7mLBI1YEyMQjie0Kh2xRj5lfZMp05HbsT8fXsl07G53w7i6h4qGWJtG 4bGjJa9c14KjMmwiK7T9kZpfVQupZV5N97379maqresqOig9DcqbWVdfIX6hSmOA0CNL rk2Ei4L5v9hK60O1j8pK5VDlrJdPRr1dBZTiTWRGckgo+uoD6r6phvU/UHH4Oph7oTkU rSknRt9yUJbglQc1NxGP006Sk2RUwJ0LIg2QlifhiAX9sLxz8bLLpU9uuFtt8FFiN+KT kLR2Xs7/3KE8cDYG231yxUeKvxovzMrMiJd1eaAkzGFeJJeAS2mbV+8FNgCMLyJ9nob4 g9/Q== X-Gm-Message-State: ACgBeo2nNnjrOzmhu6ehPfyP4Ax/2F61/GE6wIzt5yRK3qfKSGGWauIG z7NVlcELqpXPV9GAIik5z50NcN/YfxL5Dz7i X-Google-Smtp-Source: AA6agR7/1O0PUNo3AX5ZYeMviXa8A508FrWj0FY0zEydM9g80ukuhwSdzyeEaktFlacJzuPwZuqyeA== X-Received: by 2002:a05:6402:a47:b0:43d:17a0:fdc9 with SMTP id bt7-20020a0564020a4700b0043d17a0fdc9mr441190edb.41.1660244362094; Thu, 11 Aug 2022 11:59:22 -0700 (PDT) Received: from smtpclient.apple ([93.175.202.231]) by smtp.gmail.com with ESMTPSA id l28-20020a50d6dc000000b0044318cbd9b4sm119643edj.67.2022.08.11.11.59.21 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 11 Aug 2022 11:59:21 -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: Date: Thu, 11 Aug 2022 21:59:20 +0300 Cc: php internals Content-Transfer-Encoding: quoted-printable Message-ID: <5B9CECB4-2B42-4DA2-870F-A44508C0F9E3@gmail.com> References: <62f48769.250a0220.37387.53b5SMTPIN_ADDED_MISSING@mx.google.com> <01D4987C-FD37-4BD2-A1AC-DD10F808F248@gmail.com> To: Larry Garfield 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 21:42, Larry Garfield = wrote: >=20 > On Thu, Aug 11, 2022, at 4:03 AM, Alex Wells wrote: >=20 >> Besides, I believe working with existing functions is more of a = problem=20 >> than a bonus - because of the naming. You=E2=80=99d have to clutter = your code=20 >> with `array_map` calls, even though it could easily be shortened to=20= >> `map`. >=20 > A couple of trivial higher order functions and you're good to go. I = have a sampling of them here: = https://github.com/Crell/fp/blob/master/src/array.php >=20 > (Modulo PHP's inconsistency between internal/user-space functions that = force having separate functions for keyed and unkeyed arrays; any = solution is going to have to deal with that problem.) >=20 >>> 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.) >>=20 >> Well, pipe operator is another option, but it=E2=80=99s got it=E2=80=99= s downsides=20 >> compared to extension methods: >> - it's less versatile: extension methods are required to specify a=20 >> type they=E2=80=99re extending, meaning they are methods, not = functions. Hence,=20 >> two different `map` method extensions can be imported in a single = file=20 >> (given they=E2=80=99re for different types - say one for = `Collection`, the=20 >> other for `array`), unlike regular functions. I believe it=E2=80=99s = common=20 >> place to use both `Collection::map` and `array::map` in a single = file,=20 >> 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=20 >> same method names, prefixes would be required >=20 > Flipside: Pipe works on arrays and strings, which this would not. And = arrays and strings are among the most common things to be chained in = this way. (Most Collection objects are just alternate OOPy syntax = around array_map and array_filter, at the end of the day.) Pipe also = doesn't give the impression that the method is "part of" the object = (it's not), whereas extensions do, despite being effectively just an = alternate syntax for a public function that takes the object as an = argument. >=20 > And functions can be easily namespaced. >=20 > --Larry Garfield >=20 > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php >=20 Why would this not work on arrays and strings? The intention is the = exact opposite: allow extension on (almost) all types allowed in PHP. = That includes all scalars, object, iterable, array, mixed, all classes, = interfaces & enums.=20 Regarding the impression, I agree. I=E2=80=99m not sure if this is a = problem though, as this isn=E2=80=99t a problem in other languages. = I=E2=80=99m aware it=E2=80=99s not a correct to compare PHP with other = languages, but in this context PHP is facing a question that wasn=E2=80=99= t much different for other languages. This is also easily solvable by = introducing an extension member access operator - something like `->>`. = Alternatively, an IDE can just highlight a method as being from an = extension.=