Syntax Snippets
Top Levels
Declare a module. The name is used by import.
module packagename.modulename;
Import a module. Makes all public symbols in module available.
import packagename.modulename;
Declare a struct.
struct StructName {
}
Declare a class.
class ClassName {
}
Declare a class, inheriting from a parent class, and implementing an interface.
class ClassName : ParentClass, AnInterface {
}
Declare an interface.
interface AnInterface {
}
Declare an interface that inherits from an interface.
interface AnInterface : ParentInterface {
}
Declare a union.
union UnionName {
}
Declare a named enum.
enum EnumName {
EnumChild1,
EnumChild2,
}
Declare a named enum with an explicit base type.
enum EnumName : string {
EnumChild1 = "child1",
EnumChild2 = "child2",
}
Declarations
A variable with a primitive type.
var: i32;
A variable with a pointer to a primitive type.
var: i32*;
A variable with an array of a primitive type.
var: i32[];
A variable with a static array of a primitive type.
var: i32[100];
A variable with an associative array type associating strings with a primitive type.
var: i32[string];
A variable with a function that takes a string and returns a bool type.
var: fn(string) bool;
A variable with a delegate that takes a string and returns a bool type.
var: dg(string) bool;
A variable with a const primitive type, that is being assigned to.
var: const(i32) = 32;
A variable with an inferred type.
var := 32;
An alias to a primitive type.
alias myInt = i32;
A function that takes no arguments and returns void.
fn aFunction() {
}
A function that takes an argument and returns it.
fn aFunction(arg: i32) i32 {
return arg;
}
A function with an argument that has a default value.
fn aFunction(arg: string = "hello") {
}
A function using homogenous variadics.
fn aFunction(arg1: i32, strings: string[]...) {
}
A function using variadics.
fn aFunction(arg1: i32, ...) {
}
An enum declaration.
enum A = 32;
An enum declaration with an explicit type.
enum size_t A = 32;
Statements
A return statement.
return 12;
A block statement.
{
// More code here.
}
An if statement.
if (condition) {
// ...
} else if (somethingElse) {
// ...
} else {
// ...
}
An auto if assignment.
if (var := couldBeNull()) {
// var is not null
} else {
// var was null, not in this scope
}
A while loop.
while (condition) {
// ...
}
A do-while loop.
do {
// ...
} while (condition);
A for statement.
for (i: size_t = 0; i < 10; ++i) {
// ...
}
A foreach statement.
foreach (e; arr) {
// ...
}
A reverse foreach statement. Iterates from the end to the beginning.
foreach_reverse (e; arr) {
// ...
}
A switch statement.
switch (var) {
case 1:
// ...
case 2, 3, 4:
// ...
case 5 .. 10:
// ...
default:
// ...
}
A continue statement. Jump to the start of the current loop.
continue;
A break statement. Exit the current loop or case statement.
break;
Goto statements. Only in switch statements. Jump to a case.
goto case 5;
goto default;
With block.
with (AnEnum) {
var := EnumValue;
}
Try, catch, finally blocks.
try {
aFunction();
} catch(e: ExceptionA) {
// ...
} catch(e: ExceptionB) {
// ...
} finally {
// ...
}
A throw statement.
throw new Exception("exception message");
Scope statements.
scope (success) {
// do a thing when this function exits normally
}
scope (failure) {
// do a thing when this function exits via a throw statement
}
scope (exit) {
// do a thing when this function exits normally or via a throw statement
}
An assert statement.
assert(errorIfThisIsFalse, "an optional message");
Binary Expressions
Addition.
var := 2 + 3; // 5
Subtraction.
var := 2 - 3; // -1
Multiplication.
var := 2 * 3; // 6
Division.
var := 4 / 2; // 2
Modulo.
var := 5 % 3; // 2
Concatenation.
var := "hello, " ~ "world"; // "hello, world"
Logical or.
var := a || b;
Logical and.
var := a && b;
Binary or.
var := a | b;
Binary and.
var := a & b;
Binary xor.
var := a ^ b;
Equality.
var := a == b;
Non-equality.
var := a != b;
AA lookup.
pointerToElement := key in aa;
Reference equality.
var := ptra is ptrb;
Ternary expression.
var := condition ? resultIfTrue : resultIfFalse;
Unary Expressions
Address of operator.
var := &var2;
Prefix increment.
++var;
Prefix decrement.
--var;
Negative value.
var := -var2;
Negate.
var := !var2;
Bitwise complement.
var := ~var2;
New expressions.
var := new i32; // type is i32*
var2 := new ClassName(constructorArg); // type is ClassName
var3 := new i32[](5); // type is i32[]. with a length of 5
Duplication expression;
var: i32[];
// ...
var2 := new var1[..];
Type cast.
obj := cast(Object)var;
Postfix Expressions
Identifier lookup
var := anAggregate.field;
Increment.
var++;
Decrement.
var--;
Function/delegate call.
aFunction(32);
Function/delegate call with explicit named parameters.
aFunction(paramName:false);
Function call with ref parameter.
aFunction(ref arr);
Function call with out parameter.
aFunction(out arr);
Array index.
var := var2[0];
Slice expression.
var := var2[0 .. 32];
Dollar slice expression. Shorthand for the array length.
var := var2[0 .. $-1];
Method call.
someAggregate.method(32);
Other Expressions
Array literal.
var := [1, 2, 3];
Associative array literal.
var := ["key1": "value1", "key2": "value2"];
String import from file.
var := import("story.txt");
Typeid expression, retrieves TypeInfo.
var := typeid(var2);
Templates
Template struct
struct S!(T, val: i32) {
var: T = val;
}
Template instantiation.
struct SInstance = mixin S!(i32, 32);