Anonymous Functions: Difference between revisions
>JulienDethurens No edit summary |
m Text replacement - "</SyntaxHighlight>" to "</syntaxhighlight>" |
||
(18 intermediate revisions by 5 users not shown) | |||
Line 1: | Line 1: | ||
An '''anonymous function''' is a function literal that has no direct identifier. They are used in shortening code. | {{Map|Functions}} {{CatUp|Functions}} | ||
An '''anonymous function''' is a function literal that has no direct identifier. They are used in shortening code. A downside to these functions are that you can only use them in the expression they're formed in. | |||
==Examples== | ==Examples== | ||
{{Example| An anonymous function used with [[connect]]: | {{Example| An anonymous function used with [[RBXScriptSignal#Methods|connect]]: | ||
< | <syntaxhighlight lang="lua"> | ||
brick = script.Parent | brick = script.Parent | ||
brick.Touched:connect(function(part) | brick.Touched:connect(function(part) | ||
Line 10: | Line 10: | ||
print("Beep") | print("Beep") | ||
end) | end) | ||
</ | </syntaxhighlight>}} | ||
The above code is shorter than defining a named function, like such: | The above code is shorter than defining a named function, like such: | ||
{{Example|< | {{Example|<syntaxhighlight lang="lua"> | ||
brick = script.Parent | brick = script.Parent | ||
Line 21: | Line 21: | ||
brick.Touched:connect(onTouch) | brick.Touched:connect(onTouch) | ||
</ | </syntaxhighlight>}} | ||
Anonymous functions are most commonly used in [[Using_the_Chatted_Event|chat scripts]] or compound event scripts. These are used so that the arguments from the current function are still accessible. Example: | Anonymous functions are most commonly used in [[Using_the_Chatted_Event|chat scripts]] or compound event scripts. These are used so that the arguments from the current function are still accessible. Example: | ||
{{Example|< | {{Example|<syntaxhighlight lang="lua"> | ||
game.Players.PlayerAdded:connect(function(player) | game.Players.PlayerAdded:connect(function(player) | ||
player.Chatted:connect(function (msg) | player.Chatted:connect(function (msg) | ||
Line 31: | Line 31: | ||
end) | end) | ||
end) | end) | ||
</ | </syntaxhighlight> | ||
If you wanted to have the function in the Chatted connection to have a name, you would still have to use an anonymous function. It wouldn't need to be nearly as long as the actual message-processing code though: | If you wanted to have the function in the Chatted connection to have a name, you would still have to use an anonymous function. It wouldn't need to be nearly as long as the actual message-processing code though: | ||
< | <syntaxhighlight lang="lua"> | ||
function onPlayerChatted(player, msg) | function onPlayerChatted(player, msg) | ||
--thanks to the anonymous function below, this function has a `player` argument | --thanks to the anonymous function below, this function has a `player` argument | ||
Line 45: | Line 45: | ||
end) | end) | ||
end) | end) | ||
</ | </syntaxhighlight> | ||
}} | }} | ||
This is useful for shortening code, however this should be avoided in code that runs more than once. This is because the function is being defined more than once, which is unneeded because it is a literal function that is unchanging. These functions should be semantically named and used as such. | This is useful for shortening code, however this should be avoided in code that runs more than once. This is because the function is being defined more than once, which is unneeded because it is a literal function that is unchanging. These functions should be semantically named and used as such. | ||
==See Also== | |||
*[[Functions]] |
Latest revision as of 06:16, 27 April 2023
An anonymous function is a function literal that has no direct identifier. They are used in shortening code. A downside to these functions are that you can only use them in the expression they're formed in.
Examples
brick = script.Parent
brick.Touched:connect(function(part)
--I'm a comment in an anonymous function!
print("Beep")
end)
The above code is shorter than defining a named function, like such:
brick = script.Parent
function onTouch(part)
--I'm a comment in a named function!
print("Beep")
end
brick.Touched:connect(onTouch)
Anonymous functions are most commonly used in chat scripts or compound event scripts. These are used so that the arguments from the current function are still accessible. Example:
game.Players.PlayerAdded:connect(function(player)
player.Chatted:connect(function (msg)
--if this anonymous function was instead a named function outside of the anonymous function in the PlayerAdded connection,
--it would not be able to access the `player` variable
end)
end)
If you wanted to have the function in the Chatted connection to have a name, you would still have to use an anonymous function. It wouldn't need to be nearly as long as the actual message-processing code though:
function onPlayerChatted(player, msg)
--thanks to the anonymous function below, this function has a `player` argument
end
game.Players.PlayerAdded:connect(function (player)
player.Chatted:connect(function(msg)
--message processing is in the onPlayerChatted function instead of here
onPlayerChatted(player, msg)
--this function is defined much faster than the possible long message processing code
end)
end)
This is useful for shortening code, however this should be avoided in code that runs more than once. This is because the function is being defined more than once, which is unneeded because it is a literal function that is unchanging. These functions should be semantically named and used as such.