Skip to content

Commit

Permalink
doc(delitem): update for del macro
Browse files Browse the repository at this point in the history
  • Loading branch information
litlighilit committed Jan 2, 2025
1 parent a0ffe02 commit c9f45d1
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/pylib/pysugar.nim
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,18 @@ template `:=`*(name, value: untyped): untyped =
(var name = value; name)

macro del*(seqIdx: untyped) =
## - `del ls[idx]` -> `delete(ls, idx)`;
## - `del obj.attr` -> compilation-error. Nim is static-typed,
## dynamically deleting object's attributes is not allowed.
##
## Something like `ls[1:3]` is just disallowed in Nim's syntax.
##
## NOTE: Nim's del(seq, idx) is an O(1) operation,
## which moves the last element to `idx`
##[
- `del ls[idx]` -> `delitem(ls, idx)`;
- `del obj.attr` -> compilation-error. Nim is static-typed,
dynamically deleting object's attributes is not allowed.
- `del ls[1:3]` (only supported in `def` body)
- see [list of unsupported notations](
https://github.com/nimpylib/pylib/blob/master/doc%2FmustRewriteExtern%2FgetsetitemSliceLit.md)
for more details.
.. note:: Nim's `del(seq, idx)` is an O(1) operation,
which moves the last element to `idx`
]##
result = newCall(newDotExpr(ident"system", ident"del"), seqIdx)
var seqV, idx: NimNode
if seqIdx.kind == nnkBracketExpr:
Expand All @@ -47,5 +51,5 @@ macro del*(seqIdx: untyped) =
seqV = seqIdx[1]
idx = seqIdx[2]
else:
return
return # do not affect Nim system `del ls, idx`
result = newCall("delitem", seqV, idx)

0 comments on commit c9f45d1

Please sign in to comment.