Page 1 of 1

BrotSagtMist: elaborate if you can

Posted: Wed Jun 29, 2022 12:01 pm
by togFox
In another thread, you said:
Lua automatically calls tostring on numbers if the function expects strings.
Reminder that globals ARE in a table. Its "_G".
I'm unsure how lua would know a function expects strings? If it detects a string function then it does a tostring before executing the string function?

Also, can I iterate through the _G table and print all global constant names to the console (for debugging)? How? Thanks.

Re: BrotSagtMist: elaborate if you can

Posted: Wed Jun 29, 2022 12:27 pm
by MrFariator
Consider some simple situation like:

Code: Select all

function myFunc ( input )
  print ( "input: " .. input)
end
myFunc ( 5 ) -- prints: input: 5
myFunc ( "5" ) -- prints: input: 5
myFunc ( false ) -- error: attempt to concatenate local 'input' (a boolean value)
myFunc ( {} ) -- error: attempt to concatenate local 'input' (a table value)
Lua simply does some implicit type conversion from numbers to strings if applicable to the situation, like string concatenation. It won't do the conversion for all variable types and simply throws an error, though, as shown above. Ultimately, it depends on the function and its implementation, since the function may do some type checking (eq. with assert) even if it otherwise would work.

As for _G, you can traverse it just like any other table:

Code: Select all

for globalKey, globalVariableValue in pairs (_G) do
  print ( globalKey, globalVariableValue )
end

someGlobalTable = {
  -- etc
} 
for k, v in pairs (_G.someGlobalTable) do
  print ( k, v )
end

Re: BrotSagtMist: elaborate if you can

Posted: Wed Jun 29, 2022 2:35 pm
by zorg
Lua doesn't know what a function expects as parameters... however, like in the above example, the print function itself converts the arguments to string when needed... the concat operation might also do that to non-string variables/values.

Re: BrotSagtMist: elaborate if you can

Posted: Wed Jun 29, 2022 5:08 pm
by pgimeno
And the opposite is true as well:

Code: Select all

print(2 + "3") -- prints 5

Re: BrotSagtMist: elaborate if you can

Posted: Wed Jun 29, 2022 5:15 pm
by BrotSagtMist
Ninjad by Mr Fariator. That is a nice write up.

Ive been trying to keep the thread simple, _implicit type conversion_ isnt a term to throw when the person struggles to write a file.
Let alone we could actually control this behaviour using metatables and force math operations on strings and tables if we wanted to.

Regarding _G, its good for dirty solutions:

Code: Select all

function f1()
 print("hello")
end
function love.keypressed(k)
 if _G[k] then _G[k]() end
end
And we saved making a table for shortcuts. (and will make a mess later on)

Re: BrotSagtMist: elaborate if you can

Posted: Mon Jul 04, 2022 1:23 am
by Gunroar:Cannon()
pgimeno wrote: Wed Jun 29, 2022 5:08 pm And the opposite is true as well:

Code: Select all

print(2 + "3") -- prints 5
Wow, didn't know that. Nice (though can't think of any uses, it's just cool that it can).