Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116249 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 70447 invoked from network); 8 Oct 2021 08:36:41 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 8 Oct 2021 08:36:41 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 776E718054E for ; Fri, 8 Oct 2021 02:21:52 -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-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-ed1-f46.google.com (mail-ed1-f46.google.com [209.85.208.46]) (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 ; Fri, 8 Oct 2021 02:21:52 -0700 (PDT) Received: by mail-ed1-f46.google.com with SMTP id p13so34412570edw.0 for ; Fri, 08 Oct 2021 02:21:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=jtPHyZGRv9aTfCKG8/zyZbwJW1MkIuI3HFMtN0ahVzg=; b=DfwnsJ6cDr+EKd2qOQ9OabbYU6jG0TfMn/jLqfR5XZKFJ1wEoc1D5XItt/iYHPpYEf VwNV9bfhev+Hrq9XhDRnDCtMCieQflyg9sRcTdxG+J/DeT6da0Q66yAHT0V+pW3r5/1t 32aXmPTOLtO9uj+6KG7v+V488pxY0zoq8n6hGjdU9XfhofPDDvQzpZEZNmT3VoMN9/jp +PaRhgEIVcHMjngxsmxIEo/FHMW/iIFkP2149lQO3Gq3s7fu5ZL786VguY6JnfgRvmdf N/HtFgcZzUqUQdGfJ3AmzWaSjSSFYTZfEQJI3ZtBZdUYJCDUy0ZIgr0raVfIalNXmBwy fUfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=jtPHyZGRv9aTfCKG8/zyZbwJW1MkIuI3HFMtN0ahVzg=; b=brQS/e4oZICcTPTtXGR7++VAE6YXRDNW3s+QilkDw1i4J2kdRd/iILrc9BgWVvbx2I MRo0gGiVh50o2Ef7oFG4DUZhrQJwN9ouD3/5v1h72KVi1EwaUiMoy75OocIjt3vGQv2w ZU12aSCUIGw7rg0aQoeIMnQom1q9sT6Hn8wdOKVQ/y++5HOo1nGgT8Re8OmRPmgHmvsF noCuh75lCIGFw0OL3LMr7mfQZjARqtw42qlI568+tWtgsR65jyl33OmGdFgb4jW4RCNx 6NTpmqYlr7TwA1LfgaauplD9TC6BSatRlzhqjIhStOvUjFYuGh7P2T5aZNSY50rIxqy+ 1BvQ== X-Gm-Message-State: AOAM5335osOUTkE4svj0I48HPqkfm5whFeuT/woQ3QKJcPDJg+Cm8zYW uyJVJ35upjipg6QsPrsGC27BerrCf0M75H5/y7ohRfik X-Google-Smtp-Source: ABdhPJys/P8x0yifQXDomF2RAfvQk5vOdK4cbIeGyZjNX+fDPUpp9k7kjZYrShmhudipv8S4OsiL4A1SLoTvfdLCziw= X-Received: by 2002:a50:d944:: with SMTP id u4mr13382567edj.327.1633684909285; Fri, 08 Oct 2021 02:21:49 -0700 (PDT) MIME-Version: 1.0 References: <10318ac4-cd8e-ad3d-ee2f-1700b987ce6d@xakep.ru> In-Reply-To: <10318ac4-cd8e-ad3d-ee2f-1700b987ce6d@xakep.ru> Date: Fri, 8 Oct 2021 11:21:33 +0200 Message-ID: To: Nesmeyanov Kirill Cc: PHP internals Content-Type: multipart/alternative; boundary="000000000000b163ce05cdd3e82f" Subject: Re: [PHP-DEV] Change yield interpolation behaviour From: nikita.ppv@gmail.com (Nikita Popov) --000000000000b163ce05cdd3e82f Content-Type: text/plain; charset="UTF-8" On Fri, Oct 8, 2021 at 10:55 AM Nesmeyanov Kirill wrote: > Hello Internals! > > > At the moment, there is a feature/bug in the PHP that allows to use > interpolation of generators. > > ``` > > $code = << > Hello ${yield} > > EXAMPLE; > > ``` > > I suspect that initially this functionality was not thought out, but it > partially works, which allows you to implement useful functionality. > > ``` > > [$query, $params] = sql(fn() => << > SELECT * FROM users WHERE id = ${yield 42} OR id = ${yield 0xDEADBEEF} > > SQL); > > // Expected > // $query = "SELECT * FROM users WHERE id = ? OR id = ?" > // $params = [ 42, 0xDEADBEEF ] > > ``` > > When I say that the functionality was not thought out initially, I mean > the behavior of generators within strings. For example, the following > code, which should (seemingly) implement this functionality: > > ``` > > function sql(\Closure $expr) > > { > > [$generator, $params] = [$expr(), $params]; > > while ($generator->valid()) { > > $params[] = $generator->current(); // Get the value from "yield" > > $generator->send('?'); // Insert placeholder > > } > > return [$generator->getReturn()]; > > } > > ``` > > Causes an error: > > ``` > > Warning: Undefined variable $? > > ``` > > > That is, the expression "${yield 42}" expects back not the result of > this expression, but the name of the variable. Therefore, a complete and > workable implementation of such a functionality is as follows: > https://gist.github.com/SerafimArts/2e7702620480fbce6c24bc87bfb9cb0e > > > I think it makes sense to do something about it. I have two suggestions: > > 1) Forbid using "yield" inside strings > > 2) Expect not a variable name as a result of this expression, but a > substitution value. > This doesn't really have anything to do with yield. ${expr} is PHP's general variable-variable syntax, which looks up the variable with name returned by expr. The syntax also works inside strings in the form of "${expr}". Using "${yield $v}" is just a specific instance of the general pattern following the same rules. (This syntax has a special case that should be deprecated: "${label}" will be interpreted the same as "$label" instead, which is inconsistent with how it works everywhere else. This is also why ${yield} without argument will not perform a yield and instead look for a variable called $yield.) PHP unfortunately doesn't have a general expression interpolation syntax, you can only interpolate variables and certain variable-like constructs. Regards, Nikita --000000000000b163ce05cdd3e82f--