Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hsyCh-0002Hc-Ao for pgsql-hackers@arkaria.postgresql.org; Wed, 31 Jul 2019 23:40:19 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hsyCf-00055j-Co for pgsql-hackers@arkaria.postgresql.org; Wed, 31 Jul 2019 23:40:17 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hsyCf-00053b-2W for pgsql-hackers@lists.postgresql.org; Wed, 31 Jul 2019 23:40:17 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hsyCc-0000bl-RH for pgsql-hackers@lists.postgresql.org; Wed, 31 Jul 2019 23:40:16 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id x6VNeAUg030241; Wed, 31 Jul 2019 19:40:10 -0400 From: Tom Lane To: Andres Freund cc: pgsql-hackers@lists.postgresql.org Subject: Re: POC: converting Lists into arrays In-reply-to: <20190731225756.nozn3j6lgwhm6qvq@alap3.anarazel.de> References: <11587.1550975080@sss.pgh.pa.us> <20190731225756.nozn3j6lgwhm6qvq@alap3.anarazel.de> Comments: In-reply-to Andres Freund message dated "Wed, 31 Jul 2019 15:57:56 -0700" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <30239.1564616409.1@sss.pgh.pa.us> Date: Wed, 31 Jul 2019 19:40:09 -0400 Message-ID: <30240.1564616409@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Andres Freund writes: > Unfortunately foreach(ListCell *lc, ...) doesn't work with the current > definition. Which I think isn't great, because the large scopes for loop > iteration variables imo makes the code harder to reason about. Yeah, I tried to make that possible when I redid those macros, but couldn't find a way :-(. Even granting that we're willing to have a different macro for this use-case, it doesn't seem easy, because you can only put one into the first element of a for (;;). That makes the other idea (of a foreach-ish macro declaring the listcell value variable) problematic, too :-(. One idea is that we could do something like foreach_variant(identifier, list_value) { type *v = (type *) lfirst_variant(identifier); ... } where the "identifier" isn't actually a variable name but just something we use to construct the ForEachState variable's name. (The only reason we need it is to avoid confusion in cases with nested foreach's.) The lfirst_variant macro would fetch the correct value just by looking at the ForEachState, so there's no separate ListCell* variable at all. regards, tom lane