It's just convenience for me, coming over from the wonderful printf in a world filled with arcane programming hell that is the soul of C. (Why anyone still uses it is beyond me)
Anyways, CS2D's native output functions (print, msg, msg2) only takes in strings... which means that I usually have to explicitly cast the variable into a string. (seriously, type casting in the most dynamic language ever? Blasphemy!) Hence, to forgo the mind boggling internal debate of whether I should just use my python-to-lua extension (which is ironically written in C) instead of enduring the anti-lua, I just pretend that string.format is the next best shit since sliced bread.
Also, since I rarely work with consts, I usually just do the following:
some_str:format(a,b,c)
This can be further extended, for example, if we want to join a string together with "%s=%s;", we can just do the following:
delimiter = "%s=%s;"
field = {'a',1,'b',2,...}
return delimiter:rep(#field/2):format(unpack(field))
Which for me is more concise than
str=""
field = {'a',1,'b',2,...}
for i=1,#field,2 do
str = str .. field[i] .. "=" .. field[i+1] .. ";" -- // Ahhhh, C/C++ doesn't support str concatenation.... damn those languages are backwards T_T
end
return str
Lately though, I've been too lazy to actually bother with the whole :format thing so I just add
local gm = debug.getmetatable("")
gm.__mod=function(self, other)
if type(other) ~= "table" then other = {other} end
for i,v in ipairs(other) do other[i] = tostring(v) end
return self:format(unpack(other))
end
and use
"%s %s %s"%{"a","b","c"}
instead xD