-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Renaming openarray #312
Comments
We will probably need two kinds of slices. One that can be copied and one that cannot be copied. |
When the length gets me down, I just do template toOA(x, y, z): untyped = toOpenArray(x, y, z) Like the template above one could probably I will say that if you want to call it I think |
So ... you want to rename it from a long name to an equally long name? And for what exactly, the users who cannot read a basic manual? There is no evidence that newcomers have trouble to pick up |
I'm not sure about renaming but I fully agree slicing should produce a view by default. In regards to a rename: If something needs to be picked, it should be short and clear. From the top of my head: |
If we want short we can also reuse We can always defer to learning material but we can also help people by using easier names. Regarding |
Given the current design problem with first class openArrays, we might indeed need to rename openArray, we need both an immutable and a mutable variant and the planned proc `[]`(t: Table; key: K): var V
var x: Table[openArray] # but now `[]` only offers a single level of mutability!
So we I propose |
i dont like the idea of renaming it, however, i really want an overload for toOpenArray(container) that slices the whole arrays or seq length. |
I like short names (more than most) and am ok with |
Shouldn't we better reserve |
I may be wrong, but I think if we could get # template toView(x,y,z): untyped = toOpenArray(x,y,z) # just works
let x = [0, 1]
when defined(v1): # Error: type expected {or id expected if (View)}
template View = openArray
proc foo[T](x: View[T]): int = discard
echo foo(x)
when defined(v2): # C error: too many args to function
type View[T] = openArray[T]
proc foo[T](x: View[T]): int = discard
echo foo(x) # gen code calls foo_mangle(x_mangle, 2)
when defined(v3): # works, but may not inherit magic properties
type View[T] = concept a
a.len is Ordinal
a[0] is T
for x in a: x is T
proc foo[T](x: View[T]): int = discard
echo foo(x) I think
These questions also relate to symbol aliasing questions/PRs, linked to just for reference. |
I am neutral. ...but please consider that a ton of code uses |
|
Will the notion of |
I use this construct instead
|
I'm not sure how that relates to the usage I was referring to... |
It avoids openarray[T] from matching against a string. |
I was referring to this usage: type StringView = distinct openArray[char]
proc `[]`(s: string, range: Slice[int]): StringView
proc print[T](oa: openArray[T])
proc print(sv: StringView)
print("string"[0..2]) # prints "str"
print(['s', 't', 'r']) # prints "['s', 't', 'r']" |
Openarray
are a soon-to-be first-class type in Nim (#178)The name is inherited from Pascal, Modula 2, Oberon.
It corresponds to the following concepts in other languages:
span
slice
slice
slice
andRange
(andMemRange
)ArraySlice
Slice
Given that view types are becoming first-class, I propose that we also gradually change the name
openarray
to something less foreign to developers who did not grow with a Wirth language (designer of Pascal, Modula, Oberon).I think the new name should:
As we have the type names
Slice
andRange
already taken,we can't use those. Which leaves
span
like C++,ArraySlice
like swift or coming up with our own.Name
I propose the name
ArrayView
which is more descriptive thanspan
and more likely to be grasped by users coming from higher-level language than C++.It also reuses the
views
narrative in the doc and{.experimental:"views".}
pragma or compiler flag.In terms of typing it's the same as today.
Procedures
Furthermore, I suggest we rename
toOpenArray(container, start, stop)
totoView(container, start, stop)
and add an overloadtoView(container)
that slices the whole arrays or seq length.The first will significantly improve their use in low-level libraries that routinely deal with either C code (ptr + length) or buffers from memory, streams, IO, protocols, cryptography, ...
The second covers the very common use-case where we want to pass no-copy object to delegate processing, for example across a channel.
Stretch goal
Stretch goal (emphasize mine), backward compatibility is a significant concern.
Very often, especially during advent of code season, people, even experienced, are using slicing syntax with
a[start ..< stop]
instead ofa.toOpenArray(start, stop-1)
, this leads to extra allocation and GC pressure. The toOpenArray syntax is very long to type and needs this easy to forget -1 in most cases.Now that the compiler has an escape analysis, we should consider the slicing syntax to produce a view by default. We can have
a[start..<stop].clone()
to produce the oldseq[T]
if needed.Trivia
toOpenArray
is calledslice
in the compiler (with the compiler magicmSlice
)The text was updated successfully, but these errors were encountered: