With the addition of compound assignment operators would it be possible to add the ability to perform substrings with brackets. For example:
"Hello World!"[1:5] = "Hello"
With the addition of compound assignment operators would it be possible to add the ability to perform substrings with brackets. For example:
"Hello World!"[1:5] = "Hello"
We can already get a substring with string.sub
, is there any reason a substring operator would be better?
Using :
for this would be impossible, considering something like this is already valid
local a = {b=function()return"char"end}
local char = ("abc")[a:b()]
Would a:b()
call method b
on a
and index the string with the result or would it get a substring from the value of a
to the result of calling b
? Something else would have to be used, probably ,
or |
.
And how would this work with metamethods? Would it call __index with another argument or would there be a new __substring metamethod?
It’d simply be for convenience such as how this would’ve been possible before compound assignment operators were added but is more convenient now with them.
a = 5
a = a + 2
compared to
a = 5
a += 2
It also doesn’t need to be a colon but anything that’d allow substrings to be used more quickly would be simply for convenience.
I think compound assignments were a special case because they are present in practically every other language and it makes sense. Could you explain why string.sub
is inconvenient? You can also use :
notation to call the string library functions as methods on strings because all strings share the same metatable with an __index
field that points to string
.
In what way is "Hello World!"[1:5]
more convenient than ("Hello World!"):sub(1,5)
?
Ah I forgot that was possible to do with strings. Thank you!
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.