These features are only enabled in low-level contexts.
Currently, all of these features are enabled everywhere for conciseness. This may change.
Additionally, the Standard Library contains a class named LowLevel that provides various helper methods.
Low-level contexts are created by applying the lowlevel modifier to a type
declaration, method, or block.
lowlevel class A { ... }
lowlevel struct A { ... }
lowlevel void M() { ... }
lowlevel { ... }
The low-level context extends from the declaration to all statements inside. In
other words, if a method is marked lowlevel, the parameter list of that method
can use low-level exclusive features.
Structures are custom data types that pass by value and use the stack, unlike classes which are heap-allocated.
Structures only allow field declarations with no initializers. Fields within structures cannot be constants or references.
struct MyStruct {
int a;
string b;
}
Creating a new instance of a structure uses the same new keyword as classes,
but the constructor cannot be overridden and always takes no arguments:
var myInstance = new MyStruct();
Because of this, all fields must manually be written to after structure creation:
myInstance.a = 3;
myInstance.b = "Hello";
Whenever possible, a List should be used in place of C-style arrays.
int![]! v = { 1, 2, 3 };
Arrays are heap allocated and have no members. To sort or get the length of the
array,
LowLevel.Length<T>(T!) and LowLevel.Sort<T>(T!) can be used.
Arrays are runtime checked, meaning trying to access an index outside the bounds of the array will throw an exception.
It is also important to note outside of low-level contexts, an initializer list will create a
List, while inside of a low-level context, it will create an array.
Currently, initializer lists always create arrays.
int[] v = { 1, 2, 3 };
To allow for better interop, several numeric types can be used to specify
specific sizes. These being int8, uint8, int16, uint16, int32,
uint32, int64, uint64, float32, float64.
All arithmetic upcasts to int and decimal, so casting is required in cases
such as:
int32 myInt1 = 5;
int32 myInt2 = 27;
int32 myInt3 = (int32)(myInt1 | myInt2);
Unless knowing the specific size of the integer is required, use the normal
int and decimal types, which (eventually) will support specifying ranges.
The actual implementation size of int and decimal are not to be relied on as
they can change, though currently int is equivalent to int64 and decimal
is equivalent to float64.
To allow for better interop, C-style pointers and be used. Pointers are always non-nullable and can only point to non-nullable types (unless the pointed at type is heap allocated).
To get the address of a local or field, the & operator can be used:
int! myInt = 3;
int* ptr = &myInt;
To dereference the pointer, the * operator can be used:
int! myInt = 3;
int* ptr = &myInt;
int! value = *ptr; // value = 3
Pointers support any level of indirection:
int! myInt = 3;
int* ptr1 = &myInt;
int** ptr2 = &ptr1;
...
Pointers can be freely cast to reinterpret them:
void* ptr = ...;
int* myIntPtr = (int*)ptr;
int myInt = *myIntPtr;
No runtime checks are performed so this operation is inherently unsafe. Consider:
int! myInt = 3;
int* ptr = &myInt;
MyClass* ptr2 = (MyClass*)ptr;
(*ptr2).Method(); // Undefined behavior
class MyClass {
public void Method() { ... }
}
Unlike all other non-nullable types, pointers can be created without an initializer, in which case they will default to a null pointer:
int* ptr; // ptr = nullptr
The following are all equivalent:
int* ptr;
int* ptr = nullptr;
int* ptr = (int*)null;
To do arithmetic on a pointer, you must first cast it to an integer type and, do the arithmetic, then cast it back.
For example:
void* myPtr = ...;
// Offset the pointer by 8 bytes
myPtr = (void*)((int64!)myPtr + 8);
Indexing an operator will automatically offset the pointer and then dereference it:
char* myPtr = ...;
char! myChar = myPtr[10];
The above example is equivalent to:
char* myPtr = ...;
char! myChar = *((char*)((int64!)myPtr + 10 * LowLevel.SizeOf<char!>()));
Function pointers allow calling a function using a pointer to the entry point
using the stdcall calling convention.
To get the pointer to managed method, use the & operator. A function pointer
can then be called like a normal method:
var myPtr = &MyMethod;
var myInt = myPtr(); // myInt = 4
int32! MyMethod() {
return 4;
}
When not using var, the explicit function pointer type can be written as
returnType(argTypes...)*:
int32(bool, string)* myPtr = &MyMethod;
int32 MyMethod(bool arg1, string arg2) { ... }
Function pointers are treated the same as normal pointers in that they can be freely cast. This is helpful when trying to call a function given a vtable. Consider this example of calling the first function of a vtable:
void** vtable = ...;
((void()*)vtable[0])();
For clarity, the function pointer set to a temporary:
void** vtable = ...;
var MyFunction = (void()*)vtable[0];
MyFunction();
To call into a unmanaged DLL, an extern method with a DllImport attribute can
be declared and called like a typical method:
[DllImport("example.dll")]
static extern void SomeMethod();
SomeMethod();
The method is resolved at runtime, meaning if it cannot be found an exception will be thrown.
Extern methods use the UniCode char set and the stdcall calling convention.