onyx-lang / onyx

✨ The compiler and developer toolchain for Onyx
https://onyxlang.io
BSD 2-Clause "Simplified" License
576 stars 22 forks source link

Feature: Namespaces on slices and dynamic arrays #132

Closed brendanfh closed 8 months ago

brendanfh commented 8 months ago

Adds the ability for slice types ([] T) and dynamic array types ([..] T) to have "namespaces" called Slice and Array respectively that can be injected into. It also allows for methods on these types.

Now, the language is more consistent with how you interact with common data structures. Before, Map and Set could be used with methods (->get(...)) or with core.map/core.set, but arrays had to be used with the functions in core.array. Now, the following code is possible:

arr := make([..] u32)
for I in 10 do arr->push(i)  // Would have been array.push(&arr, i)

arr->delete(0)  // Would have been array.delete(&arr, 0)

println(arr) // 1 2 3 4 5 6 7 8 9

The old way is still possible. However, using the functions from core.array is not preferred, as they are not just aliases to functions in the builtin Array structure.