added example of #()
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Sat, 30 Oct 2021 01:21:03 +0000 (20:21 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Sat, 30 Oct 2021 01:21:03 +0000 (20:21 -0500)
examples/18_macros.onyx

index 69bf03b14617d5b14b3969941deeb19c613d3fb2..383ca7d4608c6c96f79b1e4f07dcd4a313933b28 100644 (file)
@@ -89,9 +89,9 @@ main :: (args: [] cstr) {
     // Because simple_code is a compile-time value, it can be passed to procedures as so:
     triple :: ($body: Code) {
         println("Running 3 times!");
-        #insert simple_code;
-        #insert simple_code;
-        #insert simple_code;
+        #insert body;
+        #insert body;
+        #insert body;
     }
 
     triple(simple_code);
@@ -100,12 +100,16 @@ main :: (args: [] cstr) {
     // compile-time parameter (the '$').
     triple_macro :: macro (body: Code) {
         println("Running 3 times in a macro!");
-        #insert simple_code;
-        #insert simple_code;
-        #insert simple_code;
+        #insert body;
+        #insert body;
+        #insert body;
     }
 
     triple_macro(simple_code);
+
+    // As another small shorthand, if a code block only contains one expression (like the code block above),
+    // then you can write by wrapping the expression in #(...).
+    triple_macro(#(println("Short code block!")));
 }
 
 #load "core/std"