Add toOpenArray[T](ptr UncheckedArray[T]) for clarity. (#9316)
* Add `toOpenArray[T](ptr UncheckedArray[T])` for clarity. `ptr array[0,T]` for some unchecked type already works but A) `UncheckedArray` seems to be the intended future way for this kind of access, and B) essentially all use cases will have a `ptr` for that kind of array source and this call signature lets callers drop the trailing `[]` corresponding to that `ptr` deref. This PR relates to issue https://github.com/nim-lang/Nim/issues/9001 . * Add a test for toOpenArray() for UncheckedArray[T]s.
This commit is contained in:
parent
14925ee8b6
commit
1b3725e395
3 changed files with 8 additions and 1 deletions
|
|
@ -106,7 +106,7 @@ proc openArrayLoc(p: BProc, n: PNode): Rope =
|
||||||
result = "($1)+($2), ($3)-($2)+1" % [rdLoc(a), rdLoc(b), rdLoc(c)]
|
result = "($1)+($2), ($3)-($2)+1" % [rdLoc(a), rdLoc(b), rdLoc(c)]
|
||||||
else:
|
else:
|
||||||
result = "($1)+(($2)-($4)), ($3)-($2)+1" % [rdLoc(a), rdLoc(b), rdLoc(c), intLiteral(first)]
|
result = "($1)+(($2)-($4)), ($3)-($2)+1" % [rdLoc(a), rdLoc(b), rdLoc(c), intLiteral(first)]
|
||||||
of tyOpenArray, tyVarargs:
|
of tyOpenArray, tyVarargs, tyUncheckedArray:
|
||||||
result = "($1)+($2), ($3)-($2)+1" % [rdLoc(a), rdLoc(b), rdLoc(c)]
|
result = "($1)+($2), ($3)-($2)+1" % [rdLoc(a), rdLoc(b), rdLoc(c)]
|
||||||
of tyString, tySequence:
|
of tyString, tySequence:
|
||||||
if skipTypes(n.typ, abstractInst).kind == tyVar and
|
if skipTypes(n.typ, abstractInst).kind == tyVar and
|
||||||
|
|
|
||||||
|
|
@ -4338,6 +4338,8 @@ when not defined(js):
|
||||||
magic: "Slice".}
|
magic: "Slice".}
|
||||||
proc toOpenArray*[T](x: openarray[T]; first, last: int): openarray[T] {.
|
proc toOpenArray*[T](x: openarray[T]; first, last: int): openarray[T] {.
|
||||||
magic: "Slice".}
|
magic: "Slice".}
|
||||||
|
proc toOpenArray*[T](x: ptr UncheckedArray[T]; first, last: int): openarray[T] {.
|
||||||
|
magic: "Slice".}
|
||||||
proc toOpenArray*[I, T](x: array[I, T]; first, last: I): openarray[T] {.
|
proc toOpenArray*[I, T](x: array[I, T]; first, last: I): openarray[T] {.
|
||||||
magic: "Slice".}
|
magic: "Slice".}
|
||||||
proc toOpenArray*(x: string; first, last: int): openarray[char] {.
|
proc toOpenArray*(x: string; first, last: int): openarray[char] {.
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@ discard """
|
||||||
1
|
1
|
||||||
2
|
2
|
||||||
3
|
3
|
||||||
|
2
|
||||||
48
|
48
|
||||||
49
|
49
|
||||||
50
|
50
|
||||||
|
|
@ -98,6 +99,10 @@ doAssertRaises(IndexError):
|
||||||
doAssertRaises(IndexError):
|
doAssertRaises(IndexError):
|
||||||
foo(toOpenArray(arrNeg, -1, -3))
|
foo(toOpenArray(arrNeg, -1, -3))
|
||||||
|
|
||||||
|
type seqqType = ptr UncheckedArray[int]
|
||||||
|
let qData = cast[seqqType](addr seqq[0])
|
||||||
|
oaFirstElm(toOpenArray(qData, 1, 3))
|
||||||
|
|
||||||
proc foo(a: openArray[byte]) =
|
proc foo(a: openArray[byte]) =
|
||||||
for x in a: echo x
|
for x in a: echo x
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue