lua-users home
lua-l archive

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


2017-06-16 21:45 GMT+02:00 Soni L. <fakedme@gmail.com>:
> I have this Lua code:
>
> local string = "" print(string.format("%X", 16777216))
>
> And it works perfectly, but I was told it's wrong? What's wrong with it? Why
> shouldn't I do this?

You have had two thorough replies from people I respect a lot,
but an overriding problem with cute code like that is that it redefines
a global to something that is not backward compatible.

Before you did that, one could add useful PERLy methods to 'string'.
Several articles on the Wiki, e.g. StringTrim. After you did that, you
must add them to 'getmetatable"".__index'.

I.e. your code is not wrong, but the trick should only be used on
purpose to discourage custom extensions to the string library.