Add direct string indexing

Right now to index a literal string you have to do:

x = ('this value is: %s'):format(y)

These parentheses are bloaty and increase visual processing debt. I want to do:

x = 'this value is: %s':format(y)
3 Likes

This suggestion is already in the RFCs: https://github.com/Roblox/luau/blob/master/rfcs/syntax-method-call-on-string-literals.md

Either way, treating a string literal syntactically the same as an identifier, function call, expressions in parenthesis could lead to allowing something like this - which could confuse programmers who used a langauge where this kind of syntax is used for string literal concatenation:
"Should this be reserved for " "string literal concatenation like in Python or Ruby? ...or should this be treated as a call to a string value?"

I’d personally modify the rule to this

exp ::=  nil | false | true | Number | `...´ | function | 
		 prefixexpstr | tableconstructor | exp binop exp | unop exp 
...
prefixexpstr ::= prefixexp | String

…and allow string literal indexing this way

functioncall ::=  prefixexp args | prefixexpstr `:´ Name args 
var ::=  Name | prefixexpstr `[´ exp `]´ | prefixexpstr `.´ Name 

ergo, "My name is %s":format(name), "to be ".consistent, and "just in case this is implemented"[1] is a valid syntax but "looks like " "string literal concatenation", "a"("b"), and "a"{b} is not.

Doesn’t this have performance issues in Luau? I heard using the string library instead of directly indexing the string is better for performance

This would probably be nice for syntax but the issue appears to apply to tables and functions where the anonymous object has to be wrapped in brackets before it can be indexed

Thanks for your comments. In the future, please don’t hesitate to bring issues like this up on the Luau repository. We took a look at implementing that and we indeed chose a different implementation route to make sure "foo" "bar" isn’t valid.

1 Like