Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:114576 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 14041 invoked from network); 25 May 2021 10:03:25 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 25 May 2021 10:03:25 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id EE30B1804E3 for ; Tue, 25 May 2021 03:14:39 -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,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-io1-f42.google.com (mail-io1-f42.google.com [209.85.166.42]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Tue, 25 May 2021 03:14:39 -0700 (PDT) Received: by mail-io1-f42.google.com with SMTP id v9so4471956ion.11 for ; Tue, 25 May 2021 03:14:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=joYld7sZRP+MrblWXUjfvyQ+1DHmGsSrH8yGf0BMOT8=; b=lxcjiHskekbpj0T0L611jhA5MPUeqtzmUGJ7XVMHg/E/qjwGv43/hUAH/3vqATN7Zd 7kI4TzhAMXzRwWEfms9f3p/MA1a7bLVdIel78edrotVnSsVLaumSsVdlDytD1y6bjOS4 I5zc18+0geS5cv9cA3rg/TAebI4vYJoX1PRFYsn4sTHZLRlvLUiY1ASmoI74n3PjjIGe PwedR3FWImiDNp7zeLJxQ7r5mSZXLoeTvIu20USovRp0f51xPS45iYzUcbMhi5i9+kj1 UDpcmVD1rXi1Y4cN7QpEMn+c1YZtEyRC2Bt+I/SOI4xfIOrkWWZRxiMz5Xu55VOk+lbk 4XxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=joYld7sZRP+MrblWXUjfvyQ+1DHmGsSrH8yGf0BMOT8=; b=PI/IrrmIZHNvbil/tIjduvsoTaYhXGc1/LkYkxsIaA7ORAerpjI2e+3vrTwH0cMoYY IPIB7i/suHfqQHLhoQoBcBqMWi5UZ6OXregE/Se30cVRBWXeST4H8sm7H5/fAUpubK4I XGasq1HZpmtWN/Ry/eyVoBq1u9HvfuV/RCnzIXuK/1e/ZpPJwe9SxrFKrn1vIKKjly2e wkLe+U60gN+orgkt41QvQ5gJ9kNQG69WOExP64u17ilbIlcun9hN1l/d52Oj78ybO2hI pTrGbXSF5PUZF+loE1YkcA3V5VX8MlnjTnbo5BBM5XBZyux5JtinVyaO8UPZbC40zhX1 ZWdg== X-Gm-Message-State: AOAM531C9ZUOHroh6Hxki+D3gvnbxs09w4kq56wdp5LF/wEtgOMHaiVp 220bvPoJAtPMjJNTlcqpa1Kv6AzILVdbTbsUA9H1b5vOCNLLqQ== X-Google-Smtp-Source: ABdhPJyje1hISKWuXH6Eia+7cb+KAquWiFzOvby5UCx3EPb6I/iq8Dw+chP3zPkdwb7yNTNX3ZqjhtHYUF3l7MFOguI= X-Received: by 2002:a5d:8d87:: with SMTP id b7mr17119329ioj.46.1621937677840; Tue, 25 May 2021 03:14:37 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 25 May 2021 12:14:26 +0200 Message-ID: To: Karoly Negyesi Cc: "internals@lists.php.net" Content-Type: multipart/alternative; boundary="00000000000022b6b605c324cb74" Subject: Re: [PHP-DEV] A little syntactic sugar on array_* function calls? From: ocramius@gmail.com (Marco Pivetta) --00000000000022b6b605c324cb74 Content-Type: text/plain; charset="UTF-8" Heyo, On Tue, May 25, 2021 at 11:20 AM Karoly Negyesi wrote: > Hi, > > I was wondering whether $array->map($somefunction) would be possible. I am > not a C programmer by any stretch but reading ZEND_VM_HOT_OBJ_HANDLER(112 > it seems to me it should be quite easy (famous last words) to find out if > object is an array and if so then > > 1. prepend the string array_ before the method name > 2. based on a small lookup table move the "object" to the right place -- > either first argument or second. > 3. do a function call instead of a method call. > > Regarding #2 by default it's the first: > > $array->flip() becomes array_flip($array) > $array->column($column_key, $index_key) becomes array_column($array, > $column_key, $index_key) > $array->merge($array2, $array3) becomes array_merge($array, $array2, > $array3) > > There'd be a small list of methods/functions where it's the second, for > example: > > $array->map($fn) becomes array_map($fn, $array) > $array->search($needle, $strict) becomes array_search($needle, $array, > $strict) > $array->key_exists($key) becomes array_key_exists($key, $array) > > (Is there even any other?) > > For phase 1 we could skip the functions which gets the first argument by > reference (walk, sort) and figure it out later. Hand waving yes but never > let perfect stand in the way of good enough :) > > Look how nicely this reads: > > $array->map($fn)->filter($fn2) > > Compared to array_filter(array_map($fn, $array), $fn2) > > I see no BC concerns here because in any previous PHP versions this is a > fatal error. I do not see any syntax ambiguity either but here I am > probably just naive. It could also be usable with user defined functions. > > What do you think? > Have you seen https://github.com/nikic/scalar_objects ? Marco Pivetta http://twitter.com/Ocramius http://ocramius.github.com/ --00000000000022b6b605c324cb74--