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

mipri mipri at minimaltype.com
Mon Dec 16 17:20:21 UTC 2019


On Monday, 16 December 2019 at 14:02:17 UTC, aliak wrote:
> On Monday, 16 December 2019 at 13:22:45 UTC, Atila Neves wrote:
>> On Monday, 16 December 2019 at 10:48:51 UTC, aliak wrote:
>>> On Monday, 16 December 2019 at 01:35:51 UTC, Walter Bright
>>> wrote:
>>>> On 12/15/2019 2:17 AM, Aliak wrote:
>>>>> To use it people will need to understand how to use d
>>>>> tuples as well.
>>>>
>>>> No, they don't.
>>>
>>> User: why can't I do this?/Why doesn't this work?
>>>
>>> string s = i"$var";
>>>
>>> Answer?
>>
>> import std.format: format;
>> string s = format(i"$var");
>
> This is non obvious. And only explainable by mentioning tuples.

It's worse than non-obvious: it's contrary to expectation. It's
something that docs are going to have to explain and justify.
It's something that people are going to want the compiler to
special case if only for a useful error message. And you get
the same problem in

   void greet(string name) {
       writeln("Hello, ", name);
   }

   void main(string[] args) {
       string title = "Mr.";
       greet(i"$title $(args[1])");
   }

Error: function myfirstdprogram.greet(string name) is not 
callable using argument types (string, string, string)

   // But I'm never calling greet with three strings? Why is it
   // saying the error is on a line with only one string?

It is, however, not that hard to explain and justify. That it
works for assigning the parameters is a little weird. That it
works for SQL is pretty cool. That it works for translation is,
actually, a very worthwhile innovation and a good reason to
keep the proposed behavior even if it's weird.

And 'tuples' in this sense do come up in other languages:

https://stackoverflow.com/questions/2322355/proper-name-for-python-operator

In terms of "reasons people get confused or doubtful and wander
away from D" I don't think it'll come anywhere close to someone
trying to make std.range and std.algorithm work while not
having a great understanding of how they're implemented or why.

Still, some way to at least auto-import and auto-use format()
would be nice (if adding to -betterC caveats). If functions
accepting a printf-style string just had a different type,

   extern(C) snprintf(scope char* s, size_t n, scope const ichar* 
format, ...);

then format() could be auto-invoked in string-typed i"" and you
get the benefits without the immediate blow to new users.

Of course then you have to justify all the different char
types, including combinations, whether idchar vs. dichar is the
valid one. It's gross, so I can understand why it's already
been mentioned and discarded as an option in the thread, but it
just seems inevitable that if DIP 1027 is added without some
kind of hack like this, that one will get added on later
anyway, after people keep complaining that D's interpolation
is wrong or weird.



More information about the Digitalmars-d mailing list