Difference between revisions of "Macro Functions"
From GiderosMobile
Line 3: | Line 3: | ||
'''Available since:''' Gideros 2017.10<br/> | '''Available since:''' Gideros 2017.10<br/> | ||
=== Description === | === Description === | ||
− | <br /> | + | <translate><br /> |
Macro Functions receive a list of tokens and output a string which will be pasted into code at compile time.<br /><br /> | Macro Functions receive a list of tokens and output a string which will be pasted into code at compile time.<br /><br /> | ||
Line 16: | Line 16: | ||
To call macro function use it's name with parenthesis as with usual functions:<br /> | To call macro function use it's name with parenthesis as with usual functions:<br /> | ||
− | name(...arguments...)<br /><br /> | + | name(...arguments...)<br /><br /></translate> |
=== Examples === | === Examples === | ||
'''Enumeration'''<br/> | '''Enumeration'''<br/> | ||
− | <source lang="lua" | + | <source lang="lua"> |
enum @ (| | enum @ (| | ||
local t = ... | local t = ... | ||
Line 33: | Line 33: | ||
print(apple, orange, melon) --> 1 2 3</source> | print(apple, orange, melon) --> 1 2 3</source> | ||
'''Turning off the print command'''<br/> | '''Turning off the print command'''<br/> | ||
− | <source lang="lua" | + | <source lang="lua"> |
print @ (| return "" |)</source> | print @ (| return "" |)</source> | ||
'''Unroll loops'''<br/> | '''Unroll loops'''<br/> | ||
− | <source lang="lua" | + | <source lang="lua"> |
dotimes @ (| | dotimes @ (| | ||
local times = table.remove(..., 1) | local times = table.remove(..., 1) |
Revision as of 13:34, 23 August 2018
Supported platforms: android, ios, mac, pc
Available since: Gideros 2017.10
Description
Macro Functions receive a list of tokens and output a string which will be pasted into code at compile time.
name @ (| ...body... |)
You should use parenthesis around markers - '|' is the preferred marker. The same marker should be used to close macro body with closing parenthesis right after it.
You can use any of these markers.
\`~ ! # $ % ^ & * / + = |
You can redefine a macro with @@.
name @@ (| ...another_body... |)
To call macro function use it's name with parenthesis as with usual functions:
name(...arguments...)
Examples
Enumeration
enum @ (|
local t = ...
local r = {}
for i = 1, #t, 2 do
table.insert(r, t[i] .. " @ " .. i // 2 + 1)
end
print(table.concat(r, " "))
return table.concat(r, " ")
|)
enum(apple, orange, melon)
print(apple, orange, melon) --> 1 2 3
Turning off the print command
print @ (| return "" |)
Unroll loops
dotimes @ (|
local times = table.remove(..., 1)
return (table.concat(..., " ").." "):rep(times)
|)
local t = {}
dotimes(10 print "Boom!")
Methods |
EventsConstants |