Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:118423 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 87179 invoked from network); 11 Aug 2022 17:19:24 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 11 Aug 2022 17:19:24 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 17D3218054A for ; Thu, 11 Aug 2022 12:21:30 -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,HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL,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-ej1-f41.google.com (mail-ej1-f41.google.com [209.85.218.41]) (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 12:21:29 -0700 (PDT) Received: by mail-ej1-f41.google.com with SMTP id gb36so35110206ejc.10 for ; Thu, 11 Aug 2022 12:21:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:from:to:cc; bh=JbnlFNsmgZEEJ2+9fciPR5ATlrF6PvDuCILzxPqxJEw=; b=hLdOUTf3KTXYwBErl7jBwUWHuRmwHi55F56A2VUO0vAb4chYXz+Zmfo5QrDKM6/Srz 2oc77ZBPVaM6C9y0yPVgj6uOf5pPRTtU3YaMzcAQMdXw7VVwK2Chk3ICYek47L+eKZ59 39St6jRmaIaH3fDWZdIND1dozPDQXtBhn+ecI6IJBVeCzT9c9zHgL/NESJDEX299xvQN I1PHYIJ4A2L8cJQ5zRhkvMbwjhdOlIHkMQTSBtEwok3HnKq1po+cLdVd84TRDlC5XpSU aF7RDOyGjVC2wKD1OxJQOvxHHpXb8qpO/MbIPn0qSHZGTh/VWWEdZmbtaJcy2A8m3Z/g OYDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:x-gm-message-state:from:to:cc; bh=JbnlFNsmgZEEJ2+9fciPR5ATlrF6PvDuCILzxPqxJEw=; b=e/vlbBmyjSnUXsknCDqeHiL7qrNTwovJVUoyaP7VkzSTHYTyqltx0RgjmE47zIq5AF UPLh6/zcIT3fl//zf3t3s/Fi2VQPDB7CkIJeF7utAnIeBrZxIwLu0QrLjNgXmV7gnFhw CEMiqffUTx8KmNGcSC306CUMcCBXsQXVfjG7Ib0OgHietpXVGaT3+IVO+TGccWKwAS8x eZE/OD7pKvklCSo4lWkqWKCuG+QmfBGRmUUyPBG479MoTtGCpXrkOmq7oakR98pCKDhs x+IJrBV0wIHmFfiF2Indvjps4CnbHva9cAMMH8JH4MFTs/VaLIDHLXZU5lU2rb2DnqUg Kk0g== X-Gm-Message-State: ACgBeo3IZ+2yWRhldOc8u6wQtHOv80PYevdP6rQFqFpgS4gz+aAs7dw/ hZ3SZ1GmNtB10MQ+gzUfSDh1JVsQlUce9PLF X-Google-Smtp-Source: AA6agR4ijWwPa7CwJiLUvcQNB5Xw7+vikAw3RChMphuFyv+gRZTMAQ4FOQTmmUFPI3vlTT0Plb8Zew== X-Received: by 2002:a17:906:84ef:b0:730:ba33:512b with SMTP id zp15-20020a17090684ef00b00730ba33512bmr372860ejb.346.1660245688470; Thu, 11 Aug 2022 12:21:28 -0700 (PDT) Received: from smtpclient.apple ([93.175.202.231]) by smtp.gmail.com with ESMTPSA id q12-20020a17090676cc00b007309d640484sm3769805ejn.91.2022.08.11.12.21.27 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 11 Aug 2022 12:21:27 -0700 (PDT) Message-ID: <9A8B1CDE-CBE1-45BB-A18D-F589BD0338EC@gmail.com> Content-Type: multipart/alternative; boundary="Apple-Mail=_492DF2EF-C262-4B01-BA85-AE1686A19076" Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3696.100.31\)) Date: Thu, 11 Aug 2022 22:21:26 +0300 In-Reply-To: Cc: php internals To: Larry Garfield References: <62f48769.250a0220.37387.53b5SMTPIN_ADDED_MISSING@mx.google.com> <01D4987C-FD37-4BD2-A1AC-DD10F808F248@gmail.com> <5B9CECB4-2B42-4DA2-870F-A44508C0F9E3@gmail.com> X-Mailer: Apple Mail (2.3696.100.31) Subject: Re: [PHP-DEV] [Concept] Extension methods From: autaut03@gmail.com (Alex Wells) --Apple-Mail=_492DF2EF-C262-4B01-BA85-AE1686A19076 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > On 11 Aug 2022, at 22:02, Larry Garfield = wrote: >=20 > On Thu, Aug 11, 2022, at 1:59 PM, Alex Wells wrote: >=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 >>=20 >> Why would this not work on arrays and strings? The intention is the=20= >> exact opposite: allow extension on (almost) all types allowed in PHP.=20= >> That includes all scalars, object, iterable, array, mixed, all = classes,=20 >> interfaces & enums.=20 >=20 > How feasible is that to implement? >=20 >> Regarding the impression, I agree. I=E2=80=99m not sure if this is a = problem=20 >> though, as this isn=E2=80=99t a problem in other languages. I=E2=80=99m= aware it=E2=80=99s not=20 >> a correct to compare PHP with other languages, but in this context = PHP=20 >> is facing a question that wasn=E2=80=99t much different for other = languages.=20 >> This is also easily solvable by introducing an extension member = access=20 >> operator - something like `->>`. Alternatively, an IDE can just=20 >> highlight a method as being from an extension. >=20 > Numerous languages also included a pipe operator to handle this use = case, so... =C2=AF\_(=E3=83=84)_/=C2=AF >=20 > --Larry Garfield >=20 > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php = Well.. I think it=E2=80=99s the rest of the feature that is hard. = Checking for types should be relatively trivial, as one could use the = same logic for extension methods as is used when checking if an argument = can be passed into a typed function parameter, but with strict_types=3D1. = So for classes, enums & interfaces it=E2=80=99s instanceof; scalars and = array - an exact type match with an exception of int value for float = type; iterable/object/mixed also checked as a function parameter. It=E2=80= =99s likely I=E2=80=99m missing some edge cases though :)= --Apple-Mail=_492DF2EF-C262-4B01-BA85-AE1686A19076--