Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
3

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

Asked by
Psudar 882 Moderation Voter
4 years ago
Edited 4 years ago

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"
1
I don't know how the Lua interpreter works. It just does. Unhumanly 152 — 4y
1
[Nobody Liked That] Psudar 882 — 4y
1
sowwy :( Unhumanly 152 — 4y

1 answer

Log in to vote
2
Answered by
pidgey 548 Moderation Voter
4 years ago
Edited 4 years ago

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.

1
Congrats on 500 rep :^] Unhumanly 152 — 4y
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. Psudar 882 — 4y
Ad

Answer this question