// this is not necessary.
#load "core/module"
+
+// To use packages, use the 'use' keyword. The 'core' package houses all of the
+// standard library functions. In the case of this program, we will just be using
+// the 'println' function to print something to the screen.
+use core
+
// Below is the function declaration for `main`. While it might look weird at first
// sight, Onyx uses a consistent syntax for declaring static "things" (functions,
// structures, enums, etc.). It is always,
// To use it, simply create one like so:
ages : Map(str, u32);
- map.init(^ages, default=0);
- defer map.free(^ages);
+ map.init(&ages);
+ defer map.free(&ages);
// Alternatively you can use the map.make function to achieve the
// same thing:
// returned when the map does not contain the requested key.
// To put an entry into the map, use the map.put procedure.
- map.put(^ages, "Dwight", 32);
- map.put(^ages, "Jim", 25);
- map.put(^ages, "Pam", 24);
+ map.put(&ages, "Dwight", 32);
+ map.put(&ages, "Jim", 25);
+ map.put(&ages, "Pam", 24);
// To retrieve an entry's value, use the map.get procedure.
- print_age :: (ages: ^Map(str, u32), name: str) {
+ print_age :: (ages: &Map(str, u32), name: str) {
age := map.get(ages, name);
printf("{}'s age is {}.\n", name, age);
}
- print_age(^ages, "Dwight");
- print_age(^ages, "Jim");
- print_age(^ages, "Pam");
- print_age(^ages, "Michael");
+ print_age(&ages, "Dwight");
+ print_age(&ages, "Jim");
+ print_age(&ages, "Pam");
+ print_age(&ages, "Michael");
// You may noticed if you ran this program that it prints Michael's
// age is 0. This is because there was entry for the key 'Michael',
// and we provided the default of '0'. To ensure that a key is in the
// map, use the map.has procedure
- println(map.has(^ages, "Dwight"));
- println(map.has(^ages, "Michael"));
+ println(map.has(&ages, "Dwight"));
+ println(map.has(&ages, "Michael"));
}
{
// You can 'use' a package. You hopefully already knew this.
- // This brings in all the symbols from the core.alloc package into
- // this scope.
+ // This allows you to use the 'core.alloc' package in this scope
+ // under the name 'alloc'.
use core.alloc
// You can also restrict and partially rename the symbols that collide
}
{
- // You can also 'use' a package that is a subpackage of another package. Here, 'string'
- // is a subpackage of 'core' and the above 'use core' makes this package
- // available for you to write code like 'string.equal(...)'. However, because it is
- // a namespace, you can 'use' it.
- use core.string
+ // You can use everything inside of a package using a '*' between the include list.
+ use core.string {*}
s := " Some string ...";
t := strip_leading_whitespace(s); // This would have to be 'string.strip_leading_whitespace' otherwise.
}
}
- {
- // You can 'use' an enum. This is great if you have a burried enum in some other namespace
- // and want to do a switch over that enum.
-
- Player :: struct {
- Color :: enum {
- Undefined;
- Red; Green; Blue;
- }
-
- color := Color.Red;
- }
-
- value := Player.{};
-
- use Player.Color;
- switch value.color {
- case Red do println("The color is red!");
- case Green do println("The color is green!");
- case Blue do println("The color is blue!");
- case #default do println("The color is unknown...");
- }
- }
-
{
// You can 'use' struct members that are structures.
// This is already explained in the struct example.
// A powerful feature of Onyx that enables macros to be very useful is code blocks.
// Code blocks allow you to capture code and treat it as a compile-time object that can
- // be passed around. To create a code blocks, simply put '#quote' in front of the block
+ // be passed around. To create a code blocks, simply put '[]' in front of the block
// of code you want to capture. Notice that this uses '::' because this is a compile-time
// value.
- simple_code :: #quote {
+ simple_code :: [] {
println("This is a code block!");
}