This is an automated archive made by the Lemmit Bot.

The original was posted on /r/programminglanguages by /u/burbolini on 2024-10-06 19:20:58+00:00.


I’m writing a compiler for my own compiled language. I’m stuck on one particular design decision: should I go the printf route, eg.

price = 300
print('$ is $ bucks.', some-action(), price)  # notice the different types

or the string interpolation route:

price = 300
print('{some-action()} is {price} bucks'). # any expression is allowed inside '{' '}'

which one looks better?


Some more details:

I’m wondering about this, because if I add string interpolation, I have less reason to add varargs (eq. print(fmt: String, …xs: Show)). If I decide to add them, string interpolation becomes less interesting to implement and might needlessly bloat the language.

The other problem is performance. The correct way to allocate things in this language is to explicitly pass an allocator to functions (zig style) - no hidden allocations. I devised a way to do typesafe interpolation without allocation by constructing a big type, like so:

StrConcat(StrConcat(some-action(), ' is '), StrConcat(price, ' bucks.'))

And the type String would actually be a typeclass/trait, which datatypes like ConstString, Integer and StrConcat implement. However, this is probably really slow, especially for things like string equality.

What are your thoughts on this?