DIP 1027---String Interpolation---Community Review Round 1

mipri mipri at minimaltype.com
Fri Dec 13 01:14:43 UTC 2019

On Friday, 13 December 2019 at 00:05:54 UTC, mipri wrote:
> 3. Actually DIP-1027 is not as bad as I thought. In particular,
> instead of documenting the feature in terms of its failures vs.
> other languages, it can say "use format() if you mainly to
> build a string" and "here's some cool stuff that this design
> lets you do, that more traditional string interpolation could
> not do."

To be clear, some cool stuff is

1. Internationalization. Normally with string interpolation in
languages, the interpolation happens before anything can see
the string, so f.e. the Perl way to do internationalization
is just

   printf(gettext("I scanned %g %s."),
          $dir_scan_count == 1 ?
            gettext("directory") : gettext("directories"),

despite "I scanned $count $directory" being the more normal
way to write that code in Perl.

2. SQL queries without SQL injection, if support for this is
added. It's precisely due to the popularity of string
interpolation that SQL injection even happens, and so again the
Perl way to do database queries is

   my $contained_sql = <<"";
   SELECT id FROM city_buildings
      WHERE  minLong >= ? AND maxLong <= ?
      AND    minLat  >= ? AND maxLat  <= ?

   my $contained = $dbh->selectcol_arrayref($contained_sql,undef,
                         $minLong, $maxLong, $minLat, $maxLat);

Normally, string interpolation is a convenience that you can
use most of the time but have to put aside when things get
advanced. Here we have a proposed string interpolation that
fits the most common case while still being useful in more
advanced cases, which is instead a slightly less convenient in
certain cases.

The most common case:

   writeln("Hello, %s.".format(name)); // currently
   writefln(i"Hello, %name.");         // DIP-1027
   println("Hello, $name.");           // Kotlin

An advanced case:

   writeln(format(_("Hello, %s!"), name)); // currently (djtext)
   writeln(i"Hello, %name!".nu_gettext);   // DIP-1027
   // I can't find a Kotlin example.

An inconvenient case:

   // currently
   string[] greetings = [
       "Hello, %s".format(name),
       "Aloha, %s".format(name),
       "Hiya, %s".format(name),
       "Salutations, %s".format(name),

   // DIP-1027
   string[] greetings = [
       i"Hello, %name".format,
       i"Aloha, %name".format,
       i"Hiya, %name".format,
       i"Salutations, %name".format,

   // Kotlin
   val greetings = listOf(
       "Hello, $name",
       "Aloha, $name",
       "Hiya, $name",
       "Salutations, $name"

Note the 'nu_gettext'. It can't be djtext's existing _() or
getdtext() functions because you can't accept the i""  string
without also accepting the parameters for the i"" string.

More information about the Digitalmars-d mailing list