String interpolation syntax being postfix bad for intellisense #771
Replies: 5 comments 20 replies
-
IMO capturing is an important operation and it's more visible when it appears at first. var1: i32 = 10;
var2: = var1&$*;
var3: = $(var1&)*; |
Beta Was this translation helpful? Give feedback.
-
Thanks! I'm listening: This is a frequently asked question, and when something is frequently asked even after it's been answered, that's data. I do want consistency, a single way to do capture (and it's always value capture). And I do want to treat interpolation as value capture -- that seems to be a valid generalization that is working well. I've seen a pattern with respect to parentheses: It appears that string interpolation will always need the parens (or equivalent), whereas other capture needs parens more often for prefix
As @msadeqhe points out, for reference (pointer) capture, requiring the parens may actually be more readable. As @foxydevloper and many others point out, prefix |
Beta Was this translation helpful? Give feedback.
-
For autocompletion outside string literals, prefix fnc1: () = {
var1: = 0;
var2: = 0;
call(: () -> i32 = $|); // Prefix $
call(: () -> i32 = |); // Postfix $
} If Also considering these type declarations: point: @struct type = {
x: i32 = 0;
y: i32 = 0;
z: i32 = 0;
}
shape: @struct type = {
center: point = ();
} I think if programmers can capture a variable without parentheses main: () = {
var1: shape = ();
// `var1` is captured.
call(: () -> i32 = $var1.center.x + $var1.center.y + $var1.center.z); // Prefix $
call(: () -> i32 = var1$.center.x + var1$.center.y + var1$.center.z); // Postfix $
// `var1.center` is captured.
call(: () -> i32 = $(var1.center).x + $(var1.center).y + $(var1.center).z); // Prefix $
call(: () -> i32 = var1.center$.x + var1.center$.y + var1.center$.z); // Postfix $
// `var1.center.x`, `var1.center.y` and `var1.center.z` are captured.
call(: () -> i32 = $(var1.center.x) + $(var1.center.y) + $(var1.center.z)); // Prefix $
call(: () -> i32 = var1.center.x$ + var1.center.y$ + var1.center.z$); // Postfix $
} So prefix |
Beta Was this translation helpful? Give feedback.
-
Personally I prefer the prefix style since it is easier to notice and read. Prefix says "interpolate the following", postfix says "whatever you read just now, interpolate that please", which is easier to miss and harder to read when slimming the code (you can't just skip the interpolation, you need to read into it). Its the similar complaint I have in rust with implicit returns, where a giant statement may be returning depending on whether there is a semicolon at the end. On another note, I know this was discussed before but I still think that curly braces are better for interpolation, since string interpolation doesn't really "capture scope", it formats the data. Braces are also used in other languages for interpolation and in std::format, making transition easier for new developers. Prefix |
Beta Was this translation helpful? Give feedback.
-
I just found it yesterday, so not too far behind. :)
Agreed. |
Beta Was this translation helpful? Give feedback.
-
Would like to add input on https://github.com/hsutter/cppfront/wiki/Design-note:-Capture
While I think capture being postfix for capturing makes a lot of sense and is reasonable, for string interpolation it would prevent the IDE from being able to immediately provide intellisense for string interpolation, since you would first be required to type
()$
, and then jump back to after(
to trigger autocompletion. I think that string interpolation would benefit from it's syntax being prefix or{ }
, since it could tell that you entered a string interpolation and provide autocomplete. I think this would fit very well with cpp2's goal of improving toolability. I don't think string interpolation and capture need to have the same syntax, they seem to be relatively different concepts, string interpolation is mainly just sugar for concatenation.Here's a demonstration from javascript to show how prefix string interpolation (as well as if it was
{ }
) allows for your IDE to trigger autocomplete immediately upon writing the prefix, which wouldn't be as convenient if it's syntax required a postfix after the brackets.Code_2023-10-22_13-44-41.mp4
Beta Was this translation helpful? Give feedback.
All reactions