cleaned up more examples
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 5 Aug 2021 12:18:43 +0000 (07:18 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 5 Aug 2021 12:18:43 +0000 (07:18 -0500)
examples/02_variables.onyx
examples/03_basics.onyx
examples/04_fixed_arrays.onyx
examples/10_switch_statements.onyx
examples/12_varargs.onyx
examples/13_use_keyword.onyx

index 98d2606a330988ff1cf29d1f09bfdc7be6a30b86..2160c4b3e7540b9c3732391f2df105155dbeb9bd 100644 (file)
@@ -18,7 +18,7 @@ main :: (args: [] cstr) {
     // inferred from the right hand side of the equals.
     bar := 15;
 
-    print("foo is %i\n", foo);
-    print("footwo is %i\n", footwo);
-    print("bar is %i\n", bar);
+    printf("foo is {}\n", foo);
+    printf("footwo is {}\n", footwo);
+    printf("bar is {}\n", bar);
 }
index c3ca6b57603cb8bb17b761bafbd359be925dd382..58e5275f3c0f1c753828381cbd397b726fbaeef9 100644 (file)
@@ -36,8 +36,8 @@ main :: (args: [] cstr) {
     foo     := 10;
     foo_ptr := ^foo;
 
-    printf("foo is %i\n", foo);
-    printf("foo_ptr is %p\n", foo_ptr);
+    printf("foo is {}\n", foo);
+    printf("foo_ptr is {}\n", foo_ptr);
 
 
     // An important type to know before proceeding is the range type. When designing
@@ -52,7 +52,7 @@ main :: (args: [] cstr) {
     // to specify the step in a range literal.
     rng := 5 .. 25;
     print("rng is ");
-    print(rng);
+    println(rng);
 
     // Onyx has 3 different array-like types:
     //  * Fixed-size arrays, written [N] <type>, where N is a compile time know integer.
@@ -67,7 +67,7 @@ main :: (args: [] cstr) {
     for i: 0 .. 10 {
         fixed_arr[i] = i * i;
     }
-    print("fixed_arr[3] is %i\n", fixed_arr[3]);
+    printf("fixed_arr[3] is {}\n", fixed_arr[3]);
 
     // A quick thing to note about fixed size arrays is that they copy their contents
     // on assignment. In this example, another_arr declares space on the stack for 10
@@ -79,7 +79,7 @@ main :: (args: [] cstr) {
 
     fixed_arr[3] = 1234;
 
-    printf("another_arr[3] is %i\n", another_arr[3]);
+    printf("another_arr[3] is {}\n", another_arr[3]);
 
 
     // Slices are a concept not unique to Onyx. They represent a pointer and a count,
@@ -92,7 +92,7 @@ main :: (args: [] cstr) {
     // of fixed_arr. It is important to know that this does NOT make a copy of the
     // data. It simply points into the same memory as fixed_arr.
     slice_arr := fixed_arr[3 .. 9];
-    print("slice_arr[3] is %i\n", slice_arr[3]);
+    printf("slice_arr[3] is {}\n", slice_arr[3]);
 
 
     // Dynamic arrays are the most common arrays used in practice. They represent a
@@ -108,8 +108,7 @@ main :: (args: [] cstr) {
         array.push(^dyn_arr, i * i * i);
     }
 
-    print("dyn_arr is ");
-    print_array(dyn_arr);
+    printf("dyn_arr is {}\n", dyn_arr);
 
 
 
@@ -155,7 +154,7 @@ main :: (args: [] cstr) {
     // for i to be 11.
 
     while i := 0; i < 10 {
-        printf("%i ", i);
+        printf("{} ", i);
         i += 1;
     } else {
         print("The while loop never ran...");
@@ -172,7 +171,7 @@ main :: (args: [] cstr) {
     // break or continue the loop that you want to. Take this example.
     while i := 0; i < 10 {
         while j := 0; j < 10 {
-            printf("%i %j\n", i, j);
+            printf("{} {}\n", i, j);
 
             if i == 1 && j == 2 do break break;
             j += 1;
index 6f005bf10c83271a3ec80f011497b34b1514c6c3..b68f91dc8e68d4d0851110b8328c0443035751a7 100644 (file)
@@ -30,13 +30,13 @@ main :: (args: [] cstr) {
     // To access certain elements of the array, simply use the array access operator,
     // [...], as so. This sets the fourth element of the array to be 1234.
     arr[3] = 1234;
-    printf("arr[3] is %i\n", arr[3]);
+    printf("arr[3] is {}\n", arr[3]);
 
     // Fixed-size arrays can be iterated over using a `for` loop as so.
     for element: arr {
-        printf("%i ", element);
+        printf("{} ", element);
     }
-    printf("\n");
+    print("\n");
 
 
     // Fixed-size arrays are passed to procedures by reference. In the following example,
@@ -50,7 +50,7 @@ main :: (args: [] cstr) {
 
     array_proc(arr);
     for element: arr {
-        printf("%i ", element);
+        printf("{} ", element);
     }
 
 
@@ -59,8 +59,8 @@ main :: (args: [] cstr) {
     // array. Slices and dynamic arrays do not operate this way.
     other_arr : [10] i32;
     other_arr = arr;
-    for other_element: other_arr do printf("%i ", other_element);
-    printf("\n");
+    for other_element: other_arr do printf("{} ", other_element);
+    print("\n");
 
     // This does mean that the following pattern of double buffering arrays (which
     // is used widely in many C programs), will work in Onyx, but it will be much
@@ -84,8 +84,8 @@ main :: (args: [] cstr) {
     // OR
     arr_lit := i32.[ 2, 3, 5, 7, 11 ];
     
-    for elem: arr_lit do printf("%i ", elem);
-    printf("\n");
+    for elem: arr_lit do printf("{} ", elem);
+    print("\n");
 
     // The type of the elements of the array is given right before the `.`. The
     // expression `u32.[ 1, 2, 3, 4, 5 ]` has the type `[5] u32`. The elements
index 0f8bc03eb4c5cd70c6301f16be0b8809eada1ac1..9c9bec6c8b2372070c070e76b257a0dc3d729dcf 100644 (file)
@@ -50,7 +50,7 @@ main :: (args: [] cstr) {
 
         case 5 {
             x := math.max(20, 50);
-            defer printf("x in case '5' is %i.\n", x);
+            defer printf("x in case '5' is {}.\n", x);
 
             if x >= 40 do fallthrough;
 
index 268d1013352a6e9161968ded057b6a10a2b85b90..34d9948e2c2b10bf879b48c2fc9a4768fb975b9f 100644 (file)
@@ -29,7 +29,7 @@ main :: (args: [] cstr) {
     typed_varargs :: (args: ..i32) {
         print("Typed variadic arguments: ");
         for x: args {
-            printf("%i ", x);
+            printf("{} ", x);
         }
         print("\n");
     }
@@ -47,12 +47,19 @@ main :: (args: [] cstr) {
     // can be achieved because in Onyx, untyped varargs know how many varargs are passed
     // through a runtime count. This means if you call vararg_get more times than there
     // are varargs, you will get empty or default results back.
+    //
+    // Most of the time with untyped varargs, you will have some kind of format
+    // specifier to indicate how many and of what type the varargs are. For example
+    // 'printf' used to use this:
+    //
+    //   printf :: (format: str, va: ...) -> void
+    //   printf("%i %f %l %d", int, float, long, double);
     untyped_varargs :: (args: ...) {
         print("Untyped variadic arguments: ");
 
         x: i32;
         while vararg_get(args, ^x) {
-            printf("%p ", x);
+            printf("{} ", cast(rawptr) x);
         }
 
         print("\n");
@@ -62,18 +69,21 @@ main :: (args: [] cstr) {
 
 
 
+    // Untyped varargs will remain in the language for now, but a much better approach
+    // has been taken to have mixed type variadic arguments, and it is called 'any'.
+    // This is a procedure that takes a variadic number of 'any's. The powerful fact
+    // about 'any' is that is contains a member called 'type', which can be used to
+    // lookup type information at runtime. 'printf' already uses this for its '{}' format
+    // specifiers, and it knows how to print type names.
+    any_varargs :: (args: ..any) {
+        print("Any variadic arguments: ");
 
-    // Most of the time with untyped varargs, you will have some kind of format
-    // specifier to indicate how many and of what type the varargs are. For example
-    // 'printf' uses this:
-    //
-    //   printf :: (format: str, va: ...) -> void
-    //   printf("%i %f %l %d", int, float, long, double);
-    //
-    // There are plans for the future to have better runtime type information available,
-    // which would allow you to introspect the types of each vararg at runtime. This
-    // would mean that instead of using '%i' style specifiers in printf, you could use
-    // '{}' to indicate the next variadic argument, whose type is automatically determined.
-    // If I'm not explaining this very well I apologize and hope that when the feature
-    // is implemented, it will make sense why it is a good idea.
+        for arg: args {
+            printf("{}, ", arg.type);
+        }
+
+        print("\n");
+    }
+
+    any_varargs(1, 2.0f, "test", any_varargs);
 }
index 7c6e5380d0e77152d8c898543414f08f79490df1..b35764010944b1cd78d0a3767bc7515c5a4402f7 100644 (file)
@@ -56,7 +56,7 @@ main :: (args: [] cstr) {
         // procedure between two areas of concern.
 
         basically_a_method :: (use dummy: ^Dummy) {
-            printf("Dummy with name '%s', and age '%i'.\n", name, age);
+            printf("Dummy with name '{}', and age '{}'.\n", name, age);
         }
 
         dummy := Dummy.{ "Phil", 15 };