String interpolation available for Studio

Somebody just tell me. This means all my pain with strings are finally gone right?

This is literally (one of) the fastest item to be out from beta. Good job on this feature! (please put the formatting on the forum :frowning_face: )

Think I found a bug with if-then-else expressions inside of interpolated strings. The script editor seems to think an extra “end” is needed when it isn’t actually needed.

local s = `{if true then 'a' else 'b' }`

2 Likes

I mean, writing like this works fine with me. Still funny how that happens though. Turning a string into a if statement lol

Code

image

Output

image

Only thing is that if-then-else expressions are supported Luau syntax.

Check here: Syntax - Luau

Off-topic rationale

Main distinction is that using normal Lua “ternary” operators sometimes doesn’t work if you want to evaluate to a falsy value.

For a very artificial example,

local obj = workspace
local a = obj and false or true

In the case that we want “a” to be false because obj is truthy, that expression will return true because it’s essentially evaluating to false or true, which will obviously all-in-all evaluate to true.

With if-then-else expressions we can get our expected result:

local obj = workspace
local a = if obj then false else true

a evaluates to false.

Press Enter after you write line 1

icic, i’ve never really used if then else in a string/variable as of and or was more easier and little more shorter than the if then else way. Still funny how they made a bug with the if then else in the backticks string.

we should start calling it a if string condition lol

1 Like

Which is to always be expected. “Raw”, or primitive, Lua functions, which are closer to C, are usually better performant-wise. Rarely is the case when declaring things on the fly will be faster.

The anomaly happens if, at some point, a new implemented function is faster than Lua’s predefined functions.

how is it unsafe? I don’t really get it. Is it the same issue as SQL injection?

1 Like

No, unsafe in the sense that if you’re trying to concatenate something that isn’t concatenable and might seem like it could be concatenable (namely booleans), it’ll error. But I suppose string.format errors too in that case

ok, I just wanted to clarify because I am not very knowledgeable in that matter and I thought a security issue could’ve been caused by doing something like

local hakr = '")'--and then do ur hacky things

print("hi" .. hakr)

… but on second thought that doesn’t make sense lol

2 Likes

Possibly a better regex feature in the future too?

Bit of a late reply, but if your doing “a” … “b” … “c” … “d” all in one statement, it concatenates them all together at once. On the other hand, yes if you spam …= it will be not good for performance.

1 Like

I think I found a bug, I’ll post it here since I can’t create a bug report post.


Autocomplete doesn’t work inside a string interpolation string if a closing } is missing, even though the syntax highlighting does work.
image

When the } isn’t missing it works like expected
image


This is a problem because pressing { doesn’t automatically create a } inside of strings (another bug?). While it does outside of strings, which means you have to think more while writing and can’t rely on muscle memory as much.

Thank you, I notified the team about this issue.

1 Like

I forgot to mention this, but are there any plans on allowing interpolated strings to be passed as literal parameters like strings and tables?

print "Hello"
print `Hello`

Granted, it’s an esoteric feature but I’ve seen libraries take advantage of it before (i.e. Fusion).

Is it possible to interpolate pre-made strings? Like this:

local errorCode = `Oh noes! There was an error: ` -- how can I put it here?

Also, is it possible to escape interpolation?

EDIT: Just for the hell of it, I did a quick benchmark test. Now lets see whos the fastest!

It looks like String Interpolation comes in first at about 0.09ms

  1. String Interpolation
  2. Concatenation
  3. String formatting

That just ruins the point of using string interpolation. It is supposed to interpolate values as the string is being created. If you need to do that then just manually concat pieces of string together or use string.format

Yes, with backslashes

print(`this escapes \{123} but this wont {123}`)

I got different results

		["string interpolation"] = function()
			for i = 1, 1e3 do
				local a = `{i}a{i+1}b{i+2}c{i+3}`
			end
		end;

		["string concat"] = function()
			for i = 1, 1e3 do
				local a = (i) ..'a'.. (i+1) ..'b'.. (i+2) ..'c'.. (i+3)
			end
		end;
		
		["string format"] = function()
			for i = 1, 1e3 do
				local a = string.format('%ia%ib%ic%i', i, i+1, i+2, i+3)
			end
		end;
		
		["table concat"] = function()
			for i = 1, 1e3 do
				local a = table.concat{i, 'a', i+1, 'b', i+2, 'c', i+3}
			end
		end;
1 Like

can we ever expect to see a string.interpolate(value, environment) function? this is a great feature as is, but it is useless if you don’t hard-code every string you want to interpolate. for example, in my game every NPC character has a large table of dialogue lines with placeholders for values like the name of the player they’re talking to, and the amount of money they are willing to pay a player for completing an objective. currently i use a separate module i wrote specifically to fill in the placeholders. i understand that a fully-featured string.interpolate function probably isn’t possible, since it would allow for arbitrary expressions to run, but even a restricted version of the string interpolation is better than string.format

Even though this feature is available, it doesn’t suit my needs to replace string.format because of its ability to turn decimals into integers without any special work.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.