Jump to page: 1 25  
Page
Thread overview
String interpolation, after a healthy debate on discord
Dec 09, 2021
WebFreak001
Dec 09, 2021
forkit
Dec 10, 2021
forkit
Dec 10, 2021
deadalnix
Dec 10, 2021
Ogi
Dec 10, 2021
Adam D Ruppe
Dec 10, 2021
WebFreak001
Dec 14, 2021
Ogi
Dec 14, 2021
WebFreak001
Dec 14, 2021
bauss
Dec 14, 2021
WebFreak001
Dec 14, 2021
zjh
Dec 14, 2021
zjh
Dec 14, 2021
rumbu
Dec 14, 2021
bachmeier
Dec 14, 2021
zjh
Dec 14, 2021
Nick Treleaven
Dec 14, 2021
deadalnix
Dec 14, 2021
zjh
Dec 14, 2021
bachmeier
Dec 14, 2021
Ogi
Dec 14, 2021
WebFreak001
Dec 14, 2021
Ogi
Dec 14, 2021
WebFreak001
Dec 14, 2021
Ogi
Dec 11, 2021
ManKey
Dec 14, 2021
rikki cattermole
Dec 14, 2021
WebFreak001
Dec 14, 2021
Adam D Ruppe
Dec 14, 2021
Daniel N
Dec 14, 2021
WebFreak001
Dec 14, 2021
Daniel N
Dec 14, 2021
Paul Backus
Dec 16, 2021
deadalnix
Dec 16, 2021
Adam D Ruppe
Dec 16, 2021
bauss
Dec 14, 2021
Abdulhaq
December 09, 2021

We had an argument on the discord about the YAIDIP proposal and a simpler syntax form identifier"" based on Ola Fosheim Grøstad's proposal, and similar to JavaScript's template string syntax.

We came to the conclusion that the simpler syntax would just be that - a simpler syntax - and still require the YAIDIP, but with that give nice improvement opportunities.

The simpler syntax would be an additional special function calling syntax (only valid for functions accepting the interpolated strings with the header as suggested by YAIDIP)

Example:

string text(T...)(T args) if (isInterpolatedString!T) { ... }

// could then be called:

text"Hello $name!";

// translates to:
text(i"Hello $name!");

// translates to:
text(InterpolationHeader!("Hello ", "name", "!"), "Hello ", name, "!");

For any new user the text(i"Hello $name!") or i"Hello $name!".text syntax could be daunting for regular use, possibly even hindering the adaption of interpolated strings for regular string usage.

Like the str.to!int syntax has proven an advantage over str.to!(int) and how foo.filter!isNumber.map!... has proven advantages over foo.filter!isNumber().map!...(), these interpolated string functions would be useful in everyday application by GC users, with phobos functions like text or format, or by any other users with custom functions, and drive adoption of the YAIDIP interpolated string suggestion in user code.

advantages of the simple syntax:

  • very easy syntax on the common GC-use of just creating a string from an interpolated string by a user: setContent(text"Hello $name!", text"Welcome on your profile, $name.")
  • familiar to JS developers having used their template strings
  • the return types of the handling functions can be regular structs (non-templated) and be used with functions accepting them to ensure type-safety and also allow exporting the functions in DLLs / static compiled libraries

disadvantages:

  • not usable in template arguments with type arguments (like foo!i"$bar $int") - need to use the i strings there
  • could clash with existing i""/r""/q"" string types - these would need to be excluded as special cases

Example how it could look using postgresql (dpq2) dub package:

before:

double d = -1234.56789012345;
string text = "first line\nsecond line";
Nullable!string nullString;
int[][] arrs = [[1, 2, 3], [4, 5, 6]];

QueryParams p;
p.sqlCommand = "SELECT "~
    "$1::double precision as double_field, "~
    "$2::text, "~
    "$3::text as null_field, "~
    "array['first', 'second', NULL]::text[] as array_field, "~
    "$4::integer[] as multi_array, "~
    `'{"float_value": 123.456,"text_str": "text string"}'::json as json_value`;

p.argsVariadic(
    d,
    text,
    nullString,
    arrs
);

auto r = conn.execParams(p);

after YAIDIP:

double d = -1234.56789012345;
string text = "first line\nsecond line";
Nullable!string nullString;
int[][] arrs = [[1, 2, 3], [4, 5, 6]];

QueryParams sql(T...)(T interpolatedString) { /* process the fields here, create string with incrementing variables, calling argsVariadic on the QueryParams object and returning it */ }

QueryParams p = sql(i`SELECT
        $d::double precision as double_field,
        $text::text,
        $nullString::text as null_field,
        array['first', 'second', NULL]::text[] as array_field,
        $arrs::integer[] as multi_array,
        '{"float_value": 123.456,"text_str": "text string"}'::json as json_value`);

auto r = conn.execParams(p);

using the simple syntax would allow you then writing sql`...` instead of sql(i`...`)

Some code-bases (internal usage in packages/programs or in company code-bases) could opt for a really tiny name like alias T = std.conv.text; string s = T"Hello $name!".

The formal definition for this short syntax could either be

InterpolatedStringCall:
    Identifier " DoubleQuotedCharacters_opt " StringPostfix_opt
    Identifier ` WysiwygCharacters_opt ` StringPostfix_opt

allowing only simple identifiers

or

InterpolatedStringCall:
    Expression " DoubleQuotedCharacters_opt " StringPostfix_opt
    Expression ` WysiwygCharacters_opt ` StringPostfix_opt

allowing any arbitrary expressions, immediately followed by a string like (foo("hello")) " world" which is then interpreted like an interpolated string.

What do you think? Would this be essential for interpolated string adoption in user code or be useless?

December 09, 2021

On Thursday, 9 December 2021 at 21:06:11 UTC, WebFreak001 wrote:

>

We had an argument on the discord about the YAIDIP proposal and a simpler syntax form identifier"" based on Ola Fosheim Grøstad's proposal, and similar to JavaScript's template string syntax.

It is nice to see that you are thinking of ways to make this more user (newbie) friendly. What you propose isn't what I suggested though, but that is fine.

(I suggested something more generic which would be similar to what you would do to support user-provided literals like 100kg, 3.14rad, 100USD and where you would validate syntax and extract parameters in a customized way (think regexp), but there are many ways to Rome. :-)

December 09, 2021

On Thursday, 9 December 2021 at 21:39:57 UTC, Ola Fosheim Grøstad wrote:

>

but there are many ways to Rome. :-)

Many ROADS…

(sigh)

December 09, 2021

On Thursday, 9 December 2021 at 21:06:11 UTC, WebFreak001 wrote:

>

What do you think? Would this be essential for interpolated string adoption in user code or be useless?

I'd be happier if interpolated strings were removed completely.

The idea that they are more readable and convenient, is entirely subjective.

IMO .. they are a completely unnecessary, additional cognitive load - as now you have to deal with the possibility of code having both composite AND interpolated strings.

Interpolated strings..be gone!

December 09, 2021

On Thursday, 9 December 2021 at 22:17:14 UTC, forkit wrote:

>

IMO .. they are a completely unnecessary, additional cognitive load - as now you have to deal with the possibility of code having both composite AND interpolated strings.

I understand your viewpoint, but imagine if you actually had a formal grammar describing all legal strings for a particular interpolated string type.

That would allow you to get high quality IDE support where you would get autocompletion, suggestions and early identification of spelling errors. Such tooling is reducing cognitive load and saving time.

December 10, 2021
On Thursday, 9 December 2021 at 22:39:13 UTC, Ola Fosheim Grøstad wrote:
>
> That would allow you to get high quality IDE support where you would get autocompletion, suggestions and early identification of spelling errors. Such tooling is reducing cognitive load and saving time.

My experience with such tooling, is that they create extra cognitive load, to the point where I can no longer use them (mostly cause I can no longer work out how to disable all the crap that comes with them).

Instead of focusing on my keystrokes, I end up seeing all kinds of things popping up, all over the place, all the f$HV time .. in all kinds of different colors!!

December 10, 2021
On Friday, 10 December 2021 at 04:08:44 UTC, forkit wrote:
> Instead of focusing on my keystrokes, I end up seeing all kinds of things popping up, all over the place, all the f$HV time .. in all kinds of different colors!!

So you turn it off and request help by hitting ctrl-space instead?

Anyway, a key advantage you get, when having appropriately typed strings, is that you can add string validation to the semantic server that adds language support for the IDE. This can save a lot of time for many programmers.

That alone is a good reason to prefer sql"" over i"".
December 10, 2021

On Thursday, 9 December 2021 at 21:06:11 UTC, WebFreak001 wrote:

>

The simpler syntax would be an additional special function calling syntax (only valid for functions accepting the interpolated strings with the header as suggested by YAIDIP)

Example:

string text(T...)(T args) if (isInterpolatedString!T) { ... }

// could then be called:

text"Hello $name!";

// translates to:
text(i"Hello $name!");

// translates to:
text(InterpolationHeader!("Hello ", "name", "!"), "Hello ", name, "!");

This is much better. You'll note that the i"" doesn't need to be backed in the language anymore with that construct and can be written as a library, if you define the rewrites the other way around.

December 10, 2021

On Thursday, 9 December 2021 at 21:06:11 UTC, WebFreak001 wrote:

>

For any new user the text(i"Hello $name!") or i"Hello $name!".text syntax could be daunting for regular use, possibly even hindering the adaption of interpolated strings for regular string usage.

Not only that, you’ll have to import std.cov or std.format whenever you want to get a string out of an istring. Kinda defeats the purpose of string interpolation as a language feature.

December 10, 2021
On Friday, 10 December 2021 at 13:51:16 UTC, Ogi wrote:
> Not only that, you’ll have to import `std.cov` or `std.format` whenever you want to get a string out of an istring.

This syntax proposal doesn't change that.

And it is trivial to make this convenient anyway.
« First   ‹ Prev
1 2 3 4 5