[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Why does this work: ('5'):rep(4) --> 5555 ?
- From: Jerome Vuarand <jerome.vuarand@...>
- Date: Wed, 10 Sep 2014 16:10:34 +0100
2014-09-10 14:58 GMT+01:00 Robert McLay <mclay@tacc.utexas.edu>:
> The question I'm asking is why a string literal can be used with string
> member function like format and rep:
>
> s = ('5'):rep(10) --> 5555555555
Why would you expect that *not* to work?
> I would have normally done:
>
> ss = '5' -- i fixed the quotes
> s = ss:rep(10)
That works too. The language syntax in the manual says in foo:rep(10),
foo is a prefixexp, which can be a variable, ss:rep(10), a function
call, f():rep(10), or any expression between parenthesis,
((2+3)..[[]]):rep(10). And the constant string '5' is an expression.
> I am unable to find this trick discussed in the "Programming in Lua" books
> or the Lua reference manual. It clearly works on lua 5.1, 5.2 and luajit
> 2.0.2. But is this something that I can assume that it will continue to
> work? Is this trick somehow buried in the manuals and I just missed it?
It's not a trick, it's part of the language.
But the language is not set in stone. You cannot assume this
syntax/feature, or any other, will continue to work in future
versions. What makes Lua small and modern is that it breaks backward
compatibility when necessary.