mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-04-27 07:33:57 +00:00
Document appender arrays and scope guards
This commit is contained in:
parent
ff24568b09
commit
f5f3f2b3e5
@ -141,6 +141,30 @@ void main(string[] args) {
|
|||||||
assert !(hewo is s);
|
assert !(hewo is s);
|
||||||
// same as
|
// same as
|
||||||
assert (hewo !is s);
|
assert (hewo !is s);
|
||||||
|
|
||||||
|
// Allocate arrays using "new array length"
|
||||||
|
int[] integers = new int[] 10;
|
||||||
|
assert(integers.length == 10);
|
||||||
|
assert(integers[0] == 0); // zero is default initializer
|
||||||
|
integers = integers ~ 5; // This allocates a new array!
|
||||||
|
assert(integers.length == 11);
|
||||||
|
|
||||||
|
// This is an appender array.
|
||||||
|
// Instead of (length, pointer), it tracks (capacity, length, pointer).
|
||||||
|
// When you append to it, it will use the free capacity if it can.
|
||||||
|
// If it runs out of space, it reallocates - but it will free the old array automatically.
|
||||||
|
// This makes it convenient for building arrays.
|
||||||
|
int[auto~] appender;
|
||||||
|
appender ~= 2;
|
||||||
|
appender ~= 3;
|
||||||
|
appender.free(); // same as {mem.free(appender.ptr); appender = null;}
|
||||||
|
|
||||||
|
// Scope variables are automatically freed at the end of the current scope.
|
||||||
|
scope int[auto~] someOtherAppender;
|
||||||
|
// This is the same as:
|
||||||
|
int[auto~] someOtherAppender2;
|
||||||
|
onExit { someOtherAppender2.free; }
|
||||||
|
|
||||||
// You can do a C for loop too
|
// You can do a C for loop too
|
||||||
// - but why would you want to?
|
// - but why would you want to?
|
||||||
for (int i = 0; i < 5; ++i) { }
|
for (int i = 0; i < 5; ++i) { }
|
||||||
|
Loading…
Reference in New Issue
Block a user