Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:117385 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 85789 invoked from network); 20 Mar 2022 12:13:56 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 20 Mar 2022 12:13:56 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id B22801804B3 for ; Sun, 20 Mar 2022 06:39: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,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-f45.google.com (mail-wr1-f45.google.com [209.85.221.45]) (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 ; Sun, 20 Mar 2022 06:39:52 -0700 (PDT) Received: by mail-wr1-f45.google.com with SMTP id r13so2509046wrr.9 for ; Sun, 20 Mar 2022 06:39:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:date:mime-version:user-agent:subject:content-language:to :references:from:in-reply-to:content-transfer-encoding; bh=bwYXl3WOIpo7gkUeNNErjkt+qF+ySTSx2WmhhOeF3cM=; b=Qc1NVlPFRoEpi/iI4cRoiNnmAydoNj+X14gnxUIq/6iHvMvZ9Xsl5K7IyLozmTybpX Xb+eZGtWyfuqwEkUs4PZQu4YHVWHmhYhqie/hXeEIJxHidxiKz747dvzQAT4aJKYS4cY RewJm/hPnNJB3mVZBeqOCnKM+HJcgamsEYWElOswBkda8VeroArk6VolSRH0iU1Ck86Z Wt3BQLuseLdNY9MyC5+PgVl/S998YLOyMFyDBaYy28lDGR7YJ75qCwQAJaOStI45xkxf uzHXvlDzv7uW+CpRD1zkdCD3GuChp8HM33oVmXDpp5VeKoIz4AoXS6rkxxxUMPpJM8X0 NuOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:references:from:in-reply-to :content-transfer-encoding; bh=bwYXl3WOIpo7gkUeNNErjkt+qF+ySTSx2WmhhOeF3cM=; b=7tVnSiBhNKpZZkj9DG04w+a6p4XS/TMr4aaCLCBcCqDCMx0u4PyVO1/lLxAFQe81Tq speUfaVvjhTCS+6Ga6xBd6Icwb+fSxgQLfAeK3KDWqIot2Ec/EbSMg5+0pzDGM/8gEgy g2p34XD37pV/rS14tL5ZbSMAI5vsgos7GVGwwoMjTU5HxjR79z9mXzF3HSM1EA6g4fpW ZfyOcUPWMcoN0vKUzut6WNrjWDahXrYPlej6RELT8MB7NjHKCT5bUl3tre6CaPGWRLGZ /J82LW6KUvj0ObBDXQvvtRCI6suu+M7VTOdaViCYEfL8japUF68X3y9pmJjHldx/B4Kl cRCw== X-Gm-Message-State: AOAM532vbAMmKZGbnEg8KxxhWAyrqTGy3c+wjhHsMINeV7X6mdQL/dLB ZPSgr7YM2aTHtgstqu8pMII8XSrlp0c= X-Google-Smtp-Source: ABdhPJzJqlOK9Q1Ey8E+5Fj5J7fR1Bp1BQu09xGsqHW3ULGXCLVNDSpkE0yytVJK0X4U0LFm+HRuOQ== X-Received: by 2002:adf:efd2:0:b0:203:db42:c56c with SMTP id i18-20020adfefd2000000b00203db42c56cmr14816591wrp.698.1647783590825; Sun, 20 Mar 2022 06:39:50 -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 n14-20020a5d588e000000b00204064a3800sm2866765wrf.51.2022.03.20.06.39.49 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 20 Mar 2022 06:39:50 -0700 (PDT) Message-ID: <244126ae-f42c-3b53-022e-e69e3e012ded@gmail.com> Date: Sun, 20 Mar 2022 13:39:47 +0000 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.7.0 Content-Language: en-GB To: internals@lists.php.net References: <4AC60E84-B2AD-43F4-9B72-92604FC7BD41@gmail.com> <73550eae-370d-115f-e440-4889e42dbc74@processus.org> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Subject: Re: [PHP-DEV] [RFC][Under discussion] Arbitrary string interpolation From: rowan.collins@gmail.com (Rowan Tommins) On 18/03/2022 17:49, Paul Dragoonis wrote: > Writing code in strings is a DX nightmare Can you expand a bit on what you mean by that? It seems to be common to assert the opposite, that string interpolation is much more convenient than the alternatives. Looking around, it seems nearly all currently-popular languages include some form of interpolation. Notable languages which don't currently support it at all are Java and Go. Rust doesn't have a generic syntax, but some built-in macros support variable-only interpolation: https://blog.rust-lang.org/2022/01/13/Rust-1.58.0.html#captured-identifiers-in-format-strings Perl, from which PHP has largely inherited its string syntax, is actually more restrictive than most. Arbitrary expressions are just about possible with a bit of hackery, but not really encouraged: https://perldoc.perl.org/perlfaq4#How-do-I-expand-function-calls-in-a-string%3F All the other languages I looked at have support for full expressions in their interpolation forms: * C# - $"x + y = {x + y}" - https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated * Dart - 'x + y = ${x + y}' - https://api.dart.dev/stable/2.16.1/dart-core/String-class.html * JavaScript - `x + y = ${x + y}` - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals * Kotlin - "x + y = ${x + y}"  - https://kotlinlang.org/docs/basic-types.html#string-templates * Python - f'x + y = {x + y}' - https://peps.python.org/pep-0498/ * Raku (Perl6) - "\$x + \$y = { $x + $y }" - https://docs.raku.org/language/quoting.html#Interpolation:_qq * Ruby - "x + y = #{x + y}" - http://ruby-for-beginners.rubymonstas.org/bonus/string_interpolation.html * Scala - s"x + y = ${$x + $y}" - https://docs.scala-lang.org/overviews/core/string-interpolation.html * Swift - "x + y = \(x + y)" - https://docs.swift.org/swift-book/LanguageGuide/StringsAndCharacters.html#ID292 Interestingly, a few languages have forms that combine arbitrary expression interpolation with printf-style modifiers. For instance, to format a float x to 3 decimal places, aligned right to width 10: * C# - $"{x,10:F3}" * Python - f"{x:10.3}" * Scala - f"$x%10.3f" It might be interesting to explore this for PHP, e.g. "{$:$x:10.3f}". If we're not sure we want it yet, we could leave the option open by making anything of the form "{$:expression:format}" a syntax error. Using a second colon would make ternary expressions slightly awkward; C# handles this by requiring them to be parenthesised, so "{$:( $test ? $x : $y )}" would be valid but "{$:$test ? $x : $y}" would not; we could use some other delimiter, but they'd probably all need something similar. PS: Reminder to all that convention on this list is to reply below not above quoted text. -- Rowan Tommins [IMSoP]