Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

String interpolation is magic because it's not entirely obvious when it happens, what scope rules are followed, if there are any side effects, if the original string is overwritten or whether a new instance is created, what happens when they are used within bodies of loops, what happens when they are used within closures etc., it just works. Sure these can be specified explicitly but they aren't obvious.

It's the kind of feature that is useful when you write an application but not that useful when you're trying to debug it.



I can understand if you don't like the syntax of string interpolation, but that argument is a cop-out.

If you're genuinely confused by it's behaviour then I'd suggest steering clear of the Printf (and even the vanilla Print/Println functions which does concatenation and automatic type conversion).

Or perhaps, a better suggestion would be to read the language specs and learn interpolation's behaviour since this "magic" is almost always well documented [hint: it's actually less complicated than Printf ;)]


I'm not really familiar with Go, but surely the answer to almost all of those can be "the same as variables in the same scope as the string"?


> It's the kind of feature that is useful when you write an application but not that useful when you're trying to debug it.

It's largely used in log messages and the like where it's used for debugging issues after the fact. I've yet to encounter a case where anyone was ever confused by the behaviour.


Right, if this gets in then I officially demand that the much more innocent ternary operator and prefix/postfix increment/decrement operators get in as well.


If I had to choose, I'd pick string interpolation.

i += 1 means an extra line, but that extra line is very clear.

Ternary vs an if-else arguably loses on clarity except for the simplest of cases.

"Today's date is #{date}. Your balance on account #{account.Number} is #{account.Balance}"

"Today's date is " + date + " Your balance on account " + account.Number + " is " + account.Balance

The second is a mess of +'s and "'s to me, not to mention the awkwardness of formatting spaces before and after each quote. The first, you write a sentence and plug in the variables where they belong.

Not saying you're wrong, just what I would choose.


It would be defined as sugar for the regular syntax "blah" + var. Not sure what specifically you find confusing.




Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: