Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:118409 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 30270 invoked from network); 11 Aug 2022 06:03:41 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 11 Aug 2022 06:03:41 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id B617C1804B5 for ; Thu, 11 Aug 2022 01:05:40 -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,NICE_REPLY_A, 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-wr1-f46.google.com (mail-wr1-f46.google.com [209.85.221.46]) (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 01:05:40 -0700 (PDT) Received: by mail-wr1-f46.google.com with SMTP id h13so20448169wrf.6 for ; Thu, 11 Aug 2022 01:05:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc; bh=/rAcncEl5mrUgossGlvMa2v807UcDs4phRf146daYD4=; b=Jv0QTblEvlwj7X7rfOiUvk4W1MqyfNNSfmGvkgjWAPrHiyT2gaocyn4Ama4lhQUzXj W7IJxoSWu+WZiQHzRthBkQKpnxOw30JVsLutw5vqCRVbKUYRYcKtQYNcsY0Ut9e48/eJ TcQ+mXvUfaKXUTQmHpsiR3l5eEGRC5YM0EAtBKrzwBfCHsG/CdUvPVtgTZBZ44wNHqck etsw+YRk8WkSyV59u8E4COLa64oSsji9wgnITuLYu2tLwmVkyT1NzEsHstAbUBaqP1Nm x+u4IRtZPjupEaABPQY+YtRETGsWqDVJe0ze4tTntE58FyJ5NDzRWnP/IEYQE+bRhGhC vSlA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc; bh=/rAcncEl5mrUgossGlvMa2v807UcDs4phRf146daYD4=; b=Ye9x/BFbefcu0FIYTcvMQ5VwPFjXtk2a963LMzoY5uu6OUHIGYUwT9DYmKGTytEdS4 gCB4/JSQ1eYHYzZoAeybcpX7wcz7xvP+88RMo2wAVvRi6CHEfL3FtXd/nW9i8BGAOQvX pVSYERXLhHMrTGowXjBhii1zPi5R2xtgin8CC2g3EqJRsq627QhAmGSZQzKkeVcJLL8U 94yXl7H0iOISR/VVQvTXQZ/DKKIqM9ppJV/MU3AXjWW/4jmL1anrQFCnJ0XjVV2Lco9l LW1ih5tBvfJHdMVxCpcV4+fef/LwMssFeqLU0hGLzgxbwG5X3gFMBCGJkVVtxBL8kiLM qM+g== X-Gm-Message-State: ACgBeo1tJ+3vTrtS6wvJK5rpaaHtRU1AVyJ//pAlUdEQiUf84rsZxQLU 40rBtnLWb0AN+Mm/mLAf8mH0/38mFOM= X-Google-Smtp-Source: AA6agR5/8USigfCnA2Rq54tTqKdzTdUX/4hrPE7QgbSazMbBYbMUxmTbikro6Akf9auuG3bNEc5iKw== X-Received: by 2002:a05:6000:168f:b0:220:748e:82c0 with SMTP id y15-20020a056000168f00b00220748e82c0mr18974470wrd.477.1660205138919; Thu, 11 Aug 2022 01:05:38 -0700 (PDT) Received: from [192.168.0.22] (cpc104104-brig22-2-0-cust548.3-3.cable.virginm.net. [82.10.58.37]) by smtp.googlemail.com with ESMTPSA id n8-20020a05600c3b8800b003a5418245b9sm6067753wms.19.2022.08.11.01.05.38 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 11 Aug 2022 01:05:38 -0700 (PDT) Message-ID: <3a3085d7-1abb-9f89-90b8-2bd725357614@gmail.com> Date: Thu, 11 Aug 2022 09:05:35 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.12.0 Content-Language: en-GB To: internals@lists.php.net References: <62f3da67.810a0220.ed5e8.1b2cSMTPIN_ADDED_MISSING@mx.google.com> <17C7EF67-298D-40E8-A872-5A9D78A7EEDA@gmail.com> <1f37ea30-249c-46fd-b99d-9f2097ed86b2@www.fastmail.com> In-Reply-To: <1f37ea30-249c-46fd-b99d-9f2097ed86b2@www.fastmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Re: [Concept] Extension methods From: rowan.collins@gmail.com (Rowan Tommins) On 11/08/2022 01:10, Larry Garfield wrote: > Would it be OK if extension methods came*before* normal methods? It would certainly be possible, and seems more logical than allowing extensions to over-ride __call but not anything else. It does lead to a very different feature, though: it means that you can take a fully working unit of code, add a "use extension" line at the top, and change the behaviour of that code. I haven't looked in detail at how other languages implement them, but my impression is that such a behaviour change would be something other than an "extension method" as normally understood. > Would that allow a compile time translation of this: > > use extension Foo:bar; > > $collection->bar($b); > > to this: > > Foo::bar($collection, $b); Unfortunately not, because the compiler doesn't know anything about the type of $collection, so doesn't know whether to apply the extension. The process would look something like this: 1. at compile-time, build a list of in-scope extension methods 2. before every method call, loop over the list, autoloading each entry if necessary 3. check each in-scope extension method in turn for an "instanceof" match against the current object 4. if one matches, despatch the call 5. if none matches, continue with the method call as normal The lookup could be optimized in various ways (particularly if the user has to list every method they want to be in scope), but as far as I can see, giving extension methods higher priority will always be worse for performance, because the lookups will happen more often. Giving them lowest priority, just above throwing an error, is effectively free, unless you're doing something very weird and care about the performance of errors. Regards, -- Rowan Tommins [IMSoP]