-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
std/views (1st class openArrray, but not escape-safe); std/pointers; fixes sequtils.applyIt #14869
Conversation
Maybe a name more descriptive than "chunks" 🤔 |
I don't like chunks too much either, but please suggest something... as described, slice would be the ideal (Slice+MSlice) name but it's already taken by
|
how about Window? EDIT: noticed, the plural is bad |
How about |
f810db8
to
fd0dabd
Compare
@alaviss lol, you read my mind, I've already pushed a commit that changes everything to std/view+View[T]+MView[T] (and expanded on the API; it's not the final version yet) |
I don't really see the point of introducing this into the standard library if a better version can be implemented instead. If people need it that badly, is rather see it as a nimble package. Once something is included in the standard library, it's difficult to remove. |
I like the name |
This is a good candidate for |
We got --experimental:views instead which was covered by an RFC. |
this might need to be split in 2 or 3 PRs (and I probably will add an RFC for std/views), but it shows how it can all fit together:
escape-safe
It's escape unsafe in the sense that the compiler won't give error when a stack address escapes its stack frame, other than that it's safe to use and solves actual problems. Later versions of the compiler can either offer a memory safe alternative or turn that type into one (and prevent stack address escaping).
View[T] vs UncheckedArray[T]
View[T] is in particular safer and much more useful than
UncheckedArray
since the length is a property; for example it allows items/mitems, as well as hosts of API's (whereasUncheckedArray
is just a disguised pointer that doesn't know it's valid range, so cannot be composed with other API's).naming
previous name in this PR was std/chunks+Chunk[T]/MChunk[T], but then I thought about View and it's much better fit. Other names I considered:
StringView
I will also add StringView which will be built on top of View[T]
std/pointers
std/pointers could also be split into its own PR; it allows a clean syntax for pointer indexing and I find it useful in many contexts
applyIt is now correct and more efficient
applyIt
is now made correct with respect to side effects, while at the same time avoiding un-needed copies, both with non-openArray and openArray types. This is made possible viaevalonceVar
. See tests. Note that I couldn't useevalOnceAs
because I needed avar
, notlet
evalOnceAs (and other procs in sequtils) can now be side-effect safe even for openArray inputs
evalOnceAs
had a caveat: it didn't work for openArray; with View[T], this can now be made correct wrt double evaluation, using same approach as I did forevalonceVar
var s {.evalonce.} = varSeq
right now I had to introduce
evalonceVar
, and I had to use it asevalonceVar(a, typ, expr)
, but in future it can be used asvar s {.evalonce.} = varSeq
pending fixing those:invalid pragma
for var pragmas inside templates timotheecour/Nim#89 (to use pragma macro inside a template)