Array Manifesto
#1044
Replies: 1 comment 2 replies
-
Could you get around the restriction of compile-time unknown array sizes if you only allow comptime variables in the slicing? (I imagine in almost all cases the slices wouldn't be dependent on measurement results) |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Here are some of my thoughts on how to move forwards with arrays.
Compile-time unknown sizes
We should allow a variant of arrays where the size is not tracked by the type system. In Guppy itself, I think it would make most sense for users by overloading the
array
type to support both of these variants:array[int]
is an array of integers whose size cannot change but is not known by the compilerarray[int, 42]
is an array of exactly 42 integersThis way, we can frame the size as an optional annotation to users instead of a totally separate type.
Conversion
array[T, n]
should implicitly coerce toarray[T]
, i.e. whereever an unsized array is required, it's also fine to pass a sized one. The other way around requires intervention from the user. Concretely, I propose the following methods on array:try_as_sized
to return sized array ornothing
if the size doesn't match. We can provide two overloads:as_sized
, the panicking version of the aboveInference
Type inference should infer the size wherever possible:
Slicing
We should also introduce Pythonic slicing of arrays. Luckily for us, slicing a list Python returns a new list, so we don't run into our usual Python semantics problem of having multiple references to the same object:
In Guppy, we can follow the usual runtime borrow checking logic we already used for array indexing: We implicitly use option-arrays and swap each sliced linear element with
None
. Our inout mechanism can will write values back afterwards. Therefore, simliar to indexing, we should allow slicing on linear elemts in inout positions:The runtime borrow checking also means that we can't have overlapping slices of linear data:
Finally, we can even infer the size of slices if all slice parameters (start, step, stop) are known.
Renaming to List
Finally, we can start resuing the builtin list syntax to talk about arrays. This would make the syntax more concise and easier to pick up for people coming from Python.
Beta Was this translation helpful? Give feedback.
All reactions