DEV Community

Horia Coman
Horia Coman

Posted on • Originally published at horia141.com on

2 2

String Interpolation - So Hot Right Now

One of my theories about modern programming languages is that they are all slowly converging towards two-to-five archetypes. And this happens not only on the level of features, but syntax, libraries and the whole ecosystems built around the languages.

That’s a broader idea, and one I’ll explore in another post. For now, I’m gonna talk about one particular instance of this phenomenon. The adoption of string interpolation by a large number of application programming languages as the way to produce textual output from program data.

By this I mean the ability to do the following:

var a = 10, b = 20;
var c = a + b;
System.Console.PrintLn($"a = {a} and b = {b} and c = a + b = {c}");

And see a = 10 and b = 20 and c = 30 as an output.

Now this is not a ground-breaking thing, but it is one of those small features which really help a programmer at the lowest abstraction levels - the line of code.

The above example is not far-removed from the classic printf format of "a = %d and b = %d and c = %d", but it is already a little bit clearer to grok. The neat thing and the full power of this feature is that one can have arbitrary code in a format expression, as long as it is an expression. Here’s a neat Python example:

o.write(f'Expires: {expire_time.timestamp()}')

For statically typed languages, checks can be done at compile time that everything is OK for the expressions in the string. Furthermore, since the format string can be analyzed by the compiler, one gets better protection than with printf-style strings.

Of the top of my head, here’s the major languages with support for this: C#, Dart, JavaScript, Python, Ruby, Perl, PHP, Tcl, all the shells basically. The major application level language without support for this is Java.

It is relatively easy to add to a language, so I expect it to make it into most application building languages in the near to medium future, as, it is mostly a syntactic transformation to common library calls.

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly — using the tools and languages you already love!

Learn More

Top comments (0)

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay