String interpolation available for Studio

[Update] January 9, 2023


Hello everyone!

Without further ado, we’re happy to release a beta feature for string interpolation! Please note, since this is a beta feature, it will only work in Studio.

String interpolation is the new syntax that allows you to create a string literal with expressions inside of that string literal. You can read about it in the RFC for string interpolation.

In short, it’s a safer and more ergonomic alternative over string.format. Here’s a quick example of a string interpolation:

local combos = {2, 7, 1, 8, 5}
print(`The lock combination is {table.concat(combos)}. Again, {table.concat(combos, ", ")}.`)
--> The lock combination is 27185. Again, 2, 7, 1, 8, 5.

String interpolation also composes well with __tostring metamethod.

local balance = setmetatable({ value = 500 }, {
    __tostring = function(self)
        return "$" .. tostring(self.value)
    end
})

print(`You have {balance}!`)
--> You have $500!

Known Limitations

  1. Script editor will not support auto-formatting string interpolations.

  2. Luau currently does not define any format specifiers for string interpolations.

  3. Luau currently does not support any_function_call`with backticks without parentheses`.

  4. Luau currently does not support backtick string literals as a type annotation, so type Foo = `Foo` is invalid.

Please post any comments or concerns you may have!


Big thanks to everyone involved!

383 Likes

This topic was automatically opened after 10 minutes.

FINALLY! String interpolation is a much needed feature, glad to see it’s finally been added to Luau.

34 Likes

This has been an RFC for so long, glad to finally see it in mainline-Luau!

11 Likes

This is a big moment for OSS on Roblox on top of being a big W for scripters.

Thank you Kampfkarren, very cool!

21 Likes

Great to see this feature came from OSS community :tada:
Congrats @Kampfkarren

17 Likes

I’ve never been a real fan of format and how it would create such a long list of arguments in my scripts, especially for large strings I have since a lot of the text I have is dependent on external data (the experiences I work on are always very data heavy). This changes the game entirely for how I can write my strings now that I can just reference my data in-string rather than make weird abstractions or rely on the ugliness that format presents when you need to do a lot of text changing.

:heart:

16 Likes

Oh. My. God.

You have no idea just how valuable something like this is. I have always wanted something akin to f strings in luau. The closest I could always do was create a function f and use lua’s no-parentheses rule to pass a string as the argument.

Now that we have actual support for this type of thing, this will really improve the efficiency and effectiveness of outputs.

Thank you so much for working on this! I can’t wait to give it a go!

12 Likes

This is going to clean up our code so much!! Thank you

8 Likes

Perfect timing! I recently ran into several issues with string.format while working on a complicated string system, but this resolves pretty much every issue I was dealing with! :tada:

Thanks Kampfkarren! :happy3: :heart:

9 Likes

So, what’s going to be the planned syntax for type literals? This is what I’m thinking in my mind:

type Foo = `Hello, {number}`
type Bar<A, B> = `Goodbye, {A} and {B}`
14 Likes

Just wondering if there are any performance benefits with this feature?

I currently use a lot of string formatting, and it would be nice if this new feature is more performant than the traditional string.format.

3 Likes

Compilation outputs to the same as ("string"):format(args), so it’ll match in speed.

9 Likes

That only works if $500 is a constant. If the value changes, you would be better off either using interpolation or string.format

4 Likes

Why a namecall, though? I thought the library functions performed faster.

4 Likes

They should be functionally equivalent in speed for this case. The reason why it compiles to ("x"):format(y) instead of string.format("x", y) is so that we can be confident a user overriding string won’t cause problems.

4 Likes

Nice to have a semi-major addition to the language by the community- congrats Kampfkarren!

I often rely on string formatting in some utility classes of mine and string interpolation getting released will just make a lot of stuff a lot easier & readable.

7 Likes

wow this is absolutely wicked, astronomical studio gameplay incoming

4 Likes

string.format is slightly faster.

local Number: number = 1000000
local Start: number = os.clock()

for Integer: number = 1, Number do
	local String: string = string.format("Hello, %s", "world!") -- 0.11846980001428165
	local String: string = ("Hello, %s"):format("world!") -- 0.12680549998185597
end

print(os.clock() - Start)

If you’re worried about users tampering with the string library, just switch to the namecall method when they use getfenv.

14 Likes

No way, @swmaniac congrats! This is super awesome.

5 Likes