Difference between revisions of "Macro Constants"

From GiderosMobile
(added example + formatting)
m (Text replacement - "</source>" to "</syntaxhighlight>")
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
 
<!-- GIDEROSOBJ:Macro Constants -->
 
<!-- GIDEROSOBJ:Macro Constants -->
'''<translate>Supported platforms</translate>:''' [[File:Platform android.png]][[File:Platform ios.png]][[File:Platform mac.png]][[File:Platform pc.png]]<br/>
+
'''Supported platforms:''' [[File:Platform android.png]][[File:Platform ios.png]][[File:Platform mac.png]][[File:Platform pc.png]]<br/>
'''<translate>Available since</translate>:''' Gideros 2017.10<br/>
+
'''Available since:''' Gideros 2017.10<br/>
  
=== <translate>Description</translate> ===
+
=== Description ===
Macro Constants can be used for string and numeric constants.
+
Another great Gideros feature is macros. Currently Lua has no constants, you just have to define variables like ''pi=3.14159''.
  
Just use @ rather than = when defining the macro.
+
Now you can define symbolic constants like this ''pi @ 3.14159'' and then simply use ''pi'' in the code as normal. When the Lua code is byte compiled, the constants are replaced with their literal values which saves CPU time.
  
You can use any of these delimiters after @ but they must be used in pairs. Numbers are auto-detected.
+
Macro Constants can be used for string and numeric constants. Just use @ rather than = when defining the macro.
 +
 
 +
You can use any of these delimiters after @ but they must be used in pairs. Numbers are auto-detected.
  
 
'''\`~ ! # $ % ^ & * / + = |'''
 
'''\`~ ! # $ % ^ & * / + = |'''
  
=== <translate>Examples</translate> ===
+
=== Examples ===
 
'''Simple examples'''
 
'''Simple examples'''
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
num1 @ &2^0&
 
num1 @ &2^0&
 
num2 @ &2^1&
 
num2 @ &2^1&
 
num3 @ &num1 + num2&
 
num3 @ &num1 + num2&
  
pi@3.14159265358979324
+
pi @ 3.14159265358979324
 
num4 @ -100.54
 
num4 @ -100.54
 
num5 @ 232
 
num5 @ 232
Line 30: Line 32:
 
world!
 
world!
 
]]
 
]]
</source>
+
</syntaxhighlight>
  
 
'''Commenting out the print command'''
 
'''Commenting out the print command'''
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
print @ |--|
 
print @ |--|
  
 
print(x, y, z, x + y, y * z) -- this line will be skipped
 
print(x, y, z, x + y, y * z) -- this line will be skipped
</source>
+
</syntaxhighlight>
 +
 
 +
'''Advanced example'''
 +
<syntaxhighlight lang="lua">
 +
pi @ |math.atan(1.0)*4|
 +
</syntaxhighlight>
  
 
{|-
 
{|-
 
| style="width: 50%; vertical-align:top;"|
 
| style="width: 50%; vertical-align:top;"|
=== <translate>Methods</translate> ===
+
=== Methods ===
 
| style="width: 50%; vertical-align:top;"|
 
| style="width: 50%; vertical-align:top;"|
=== <translate>Events</translate> ===
+
=== Events ===
=== <translate>Constants</translate> ===
+
=== Constants ===
 
|}
 
|}
  
 
{{GIDEROS IMPORTANT LINKS}}
 
{{GIDEROS IMPORTANT LINKS}}

Latest revision as of 14:31, 13 July 2023

Supported platforms: Platform android.pngPlatform ios.pngPlatform mac.pngPlatform pc.png
Available since: Gideros 2017.10

Description

Another great Gideros feature is macros. Currently Lua has no constants, you just have to define variables like pi=3.14159.

Now you can define symbolic constants like this pi @ 3.14159 and then simply use pi in the code as normal. When the Lua code is byte compiled, the constants are replaced with their literal values which saves CPU time.

Macro Constants can be used for string and numeric constants. Just use @ rather than = when defining the macro.

You can use any of these delimiters after @ but they must be used in pairs. Numbers are auto-detected.

\`~ ! # $ % ^ & * / + = |

Examples

Simple examples

num1 @ &2^0&
num2 @ &2^1&
num3 @ &num1 + num2&

pi @ 3.14159265358979324
num4 @ -100.54
num5 @ 232
num6 @ 444.10
str1 @ 'hello'
str2 @ "world"
str3 @ [[
Hello,
world!
]]

Commenting out the print command

print @ |--|

print(x, y, z, x + y, y * z) -- this line will be skipped

Advanced example

pi @ |math.atan(1.0)*4|

Methods

Events

Constants