lua-users home
lua-l archive

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

There are two problems with your idea:

— not only print would see the replaced 'tostring', but also anything
else, unrelated code that would be highly surprised by the changed

— print probably does not call 'tostring' for strings to begin with,
it's already a string.

To make your idea work you would have to modify the globals table,
which is likely to be a bad idea.

You may want to implement your own serialization function, and not try
to use print.


> On Nov 30, 2014, at 19:04, Thiago L. <> wrote:
> On 01/12/14 12:50 AM, Tim Hill wrote:
>>> On Nov 30, 2014, at 4:58 PM, Thiago L. <> wrote:
>>> Issues:
>>> function() return ("").sub end -- Issue number 1: How am I supposed to get an environment out of this thing from the string metatable? (seeing as there's no _ENV and no way to access the env of the scope that created this function)
>> Not sure I understand this issue. The function will of course use whatever environment that was appropriate to it (in this case, the environment of the enclosing function). What do you want it to do?
> There is no environment. Feel free to inspect it with debug.*.
>>> do
>>>  local type = type
>>>  local _ENV = {print=print, tostring = function(i) if type(i) == "string" then return ("%q"):format(i) end return ("%s"):format(i) end}
>>>  print("Issue number 2: Uh oh! This doesn't get serialized!")
>>> end
>> Again, what is the issue here? What problem are you trying to address?
> print() isn't calling my environment's tostring().
>> —Tim
> --
> Disclaimer: these emails are public and can be accessed from <TODO: get a non-DHCP IP and put it here>. If you do not agree with this, DO NOT REPLY.