BrotSagtMist: elaborate if you can

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
togFox
Party member
Posts: 770
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

BrotSagtMist: elaborate if you can

Post 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.
Current project:
https://togfox.itch.io/backyard-gridiron-manager
American football manager/sim game - build and manage a roster and win season after season
MrFariator
Party member
Posts: 509
Joined: Wed Oct 05, 2016 11:53 am

Re: BrotSagtMist: elaborate if you can

Post 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
User avatar
zorg
Party member
Posts: 3436
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: BrotSagtMist: elaborate if you can

Post 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.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: BrotSagtMist: elaborate if you can

Post by pgimeno »

And the opposite is true as well:

Code: Select all

print(2 + "3") -- prints 5
User avatar
BrotSagtMist
Party member
Posts: 607
Joined: Fri Aug 06, 2021 10:30 pm

Re: BrotSagtMist: elaborate if you can

Post 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)
obey
User avatar
Gunroar:Cannon()
Party member
Posts: 1085
Joined: Thu Dec 10, 2020 1:57 am

Re: BrotSagtMist: elaborate if you can

Post 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).
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
Post Reply

Who is online

Users browsing this forum: No registered users and 39 guests