Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Whats happening when you use strings to fill arguments in functions?

I noticed something kinda weird about print() in roblox, or really just something that isnt very intuitive to me.

In lua, stuff like math or string are tables that contain functions related to them.

If you do something like print(type(print) you get a function. Furthermore, how come I can do stuff like this?

print"yo"
print[[yo]]

Even though I'm not calling the print function like this, print(); how does the script know the difference between calling the function with () and me just writing the name of the function?

Ive also noticed you can only do it with strings (afaik) so you can only do things like this:

local function epicPrintWrapper(String)
	print(String)
end

epicPrintWrapper"Heyy"

3 comments

1

I don't know how the Lua interpreter works. It just does.

1

[Nobody Liked That]

1

sowwy :(

1 answer

✓ Accepted answer

That is just how the developer made the syntax. You can call any function with an argument type of string or table without it being encased in parentheses. Lua knows the difference because it was explicitly written in its source.

some sugar

function foo(tbl)
	return tbl[1] * 2
end
local x = foo{2} --> 4

function bar(str)
	return str .. "1"
end
local y = bar"hello" --> hello1


print"hello " .. "sir" --> compilation error
print("hello " .. "sir") -- >> hello sir
print"hello sir" -- >> hello sir

Remember that you can only call a function in Lua like this with only 1 required argument, and that you can only do it with string's or table's.

2 comments

1

Congrats on 500 rep :^]

1

Yeah, I guess that makes sense. I guess if I really wanted to understand it, I'd need to know how functions are written internally when it comes to arguments. But hey, thanks.

This discussion is preserved as it was. New answers, comments, and votes are closed.