harec

[hare] Hare compiler, written in C11 for POSIX OSs
Log | Files | Refs | README | LICENSE

22-delete.ha (778B)


      1 use rt;
      2 
      3 type my_slice = []int;
      4 
      5 fn index() void = {
      6 	let x: []int = alloc([1, 2, 3, 4, 5]);
      7 	let y = &x: *my_slice;
      8 
      9 	delete(x[1]);
     10 	assert(len(x) == 4);
     11 	assert(x[0] == 1 && x[1] == 3 && x[2] == 4 && x[3] == 5);
     12 
     13 	static delete(y[3]);
     14 	assert(len(x) == 3);
     15 	delete(y[0]);
     16 	assert(len(x) == 2);
     17 	assert(x[0] == 3 && x[1] == 4);
     18 	free(x);
     19 };
     20 
     21 fn slice() void = {
     22 	let x: my_slice = alloc([1, 2, 3, 4, 5]);
     23 	let y = &x;
     24 	const s = y: *rt::slice;
     25 
     26 	delete(x[..3]);
     27 	assert(len(x) == 2);
     28 	assert(x[0] == 4 && x[1] == 5);
     29 
     30 	delete(x[len(x)..]);
     31 
     32 	static delete(y[..]);
     33 	assert(len(x) == 0);
     34 	assert(s.capacity < 5);
     35 
     36 	append(x, [6, 7, 8, 9]...);
     37 	delete(x[1..3]);
     38 	assert(len(x) == 2);
     39 	assert(x[0] == 6 && x[1] == 9);
     40 	free(x);
     41 };
     42 
     43 export fn main() void = {
     44 	index();
     45 	slice();
     46 };