lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Thanks all.  It is clear that this is safe to use.  At least until there is a good reason to remove support for it in later releases. 

I get that ("5",x,y) is going to return "5".  What I don't get is why then is:

   s = '5':rep(4)

does not work but 

   s = ('5'):rep(4)

does.

Thanks again for shining any light in this direction.
R.




On Wed, Sep 10, 2014 at 10:10 AM, Jerome Vuarand <jerome.vuarand@gmail.com> wrote:
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.




--
Robert McLay, Ph.D.
TACC
Manager, HPC Software Tools
(512) 232-8104