C has a very relaxed approach to typing, allowing one to synthesize a pointer to anywhere in memory and call it anything one likes via code like
float* fp = (float*) 0x1245;
This is often used to advantage in such situations as in writing memory allocators and garbage collectors, where typed values are appearing, moving and disappearing.
A typesafe language like Mythryl cannot be quite that relaxed about pointers. That is one reason the Mythryl garbage collector is written in C, not Mythryl.
C code also often uses pointer arithmetic to refer to a substring of a given string:
char* long_string = "abcdefghijklmnopqrstuvwxyz"; char* last_half = long_string + 13;
In Mythryl we can and do provide a functionally similar way to create and use references to substrings and subvectors. Mythryl calls these references slices.
Apis supporting vector slicing include:
The Substring api is closely related.
Packages implementing vector slices include:
The substring package is closely related.
Slices optimize time/space performance at the cost of increased code complexity. Like all such optimizations, they should be used only if you have working code which clearly has a substantial performance problem. Absent that, it is better to just create new vectors as needed.
In general slices behave just like the underlying vectors once created:
linux$ my eval: vector = vector::from_list (0..9); #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] eval: package vs = vector_slice; # Short synonym. eval: slice = vs::make_slice (vector, 5, THE 4); # Offset 5, length 4. eval: for (i = 0; i < vs::length slice; ++i) printf "%d: %d\n" i (vs::get (slice, i)); 0: 5 1: 6 2: 7 3: 8 eval: vs::to_vector slice; # Create new vector holding copy of slice contents. #[5, 6, 7, 8]
For variety, here is the same example done with a slice of a float64_vector:
linux$ my eval: vector = vector_of_eight_byte_floats::from_list (map float64::from_int (0..9)); #[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0] eval: package vs = vector_slice_of_eight_byte_floats; eval: slice = vs::make_slice (vector, 5, THE 4); # Create a slice pointing into above vector. eval: for (i = 0; i < vs::length slice; ++i) printf "%d: %f\n" i (vs::get (slice, i)); 0: 5.000000 1: 6.000000 2: 7.000000 3: 8.000000 eval: vs::to_vector slice; # Create new vector holding copy of slice contents. #[5.0, 6.0, 7.0, 8.0]