Types
Booleans
Either true
or false
bool aBoolean = true;
null
and void
str? maybe = null;
{str: void} mapToNothing = { "hello": void };
The difference between null
and void
is a semantic one. null
is mainly useful to handle the absence of data with optionals whereas void
is mainly used to specify that a function returns nothing.
Numbers
Numbers can either be int
(32 bits integers) or float
(64 bits floating point).
int aNumber = 23; | Decimal notation
aNumber = 0b110; | Binary notation
aNumber = 0xA12F; | Hexadecimal notation
aNumber = 'A'; | Char notation
float aFloat = 23.123;
| You can embed a `_` anywhere in a number literal except for the start and end
var myLargeNumber = 1_000_000;
float
and int
can be compared without casting but are otherwise not compatible with each other.
Strings
str
represents an immutable sequence of bytes. Buzz makes no assumption about the content of a string.
str aString = "hello world";
Strings can span accross multiple lines when using the ` delimiters:
str multiline = `
i'm on several
lines
yes
`;
Interpolation
Interpolations are expressions delimited by braces within a string:
int age = 37;
str msg = "Hello there, I'm {age} years old";
Escaping
"here's a new line: \n";
"here's a tab: \t";
"here's a brace: \{";
"here's a explicit byte \123";
User data
User data are buzz values that wrap a pointer to foreign data. They are mainly used when binding to native code.
Patterns
Patterns are PCRE regexes. They are commonly used so chances are you are already familiar with them. You can otherwise read the documentation. Patterns have their own buzz value type because they wrap a compiled PCRE regex. Arguably, we could lazily compile them at runtime but this would go against the philosophy of buzz which is to prevent runtime errors that could have been detected at compile time. Patterns are delimited with $"..."
. To use "
in the pattern, escape it with \
.
pat aPattern = $"hello [a-z]+";
Data structures
Lists
Lists are a sequence of a given type.
[str] words = ["hello", "world", "yes"];
Ranges
Ranges are useful to create quick list of integers. They can also be used in foreach
statements:
foreach (int i in 0..n) {
| ...
}
| Boundaries can be descendant
rg range = n..0;
| You can make a list from it
var list = range.toList(); | -> [n, n-1, ... , 0]
| You can access its boundaries
std.print("My range is {range.low}..{range.high}");
Maps
Maps are key-value records. Order is not guaranted.
{str: int} aMap = {
"one": 1,
"two": 2,
"three": 3,
};
any
A variable typed with any
can hold any value.
any anything = "hello";
anything = 12;
anything = true;
You can't do much with that kind of variable except passing it around. In order to actually use the underlying value, you have to cast it back to a concrete type.
any anything = "hello";
if (anything as str aString) {
print(aString);
}
You can also use the as?
notation which will result in a null
if the value is not of the expected type:
any anything = "hello";
int? something = anything as? int; | -> null
int somethingElse = (anything as? int) ?? 0; | Using `??` to get a default value
Type values
In buzz, types can be manipulated like any other values. You can get the type of a value using the typeof
operator.
type myType = <[str]>;
[str] list = ["one", "two", "three"];
typeof list == myType;
Foreign structs and unions
Special types to hold foreign data, see FFI.