topenarray.nim 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. discard """
  2. targets: "c cpp js"
  3. """
  4. proc fn1[T](a: openArray[T]): seq[T] =
  5. for ai in a: result.add ai
  6. proc fn2[T](a: var openArray[T]): seq[T] =
  7. for ai in a: result.add ai
  8. proc fn3[T](a: var openArray[T]) =
  9. for i, ai in mpairs(a): ai = i * 10
  10. proc main =
  11. var a = [1,2,3,4,5]
  12. doAssert fn1(a.toOpenArray(1,3)) == @[2,3,4]
  13. doAssert fn2(toOpenArray(a, 1, 3)) == @[2,3,4]
  14. doAssert fn2(a.toOpenArray(1,3)) == @[2,3,4]
  15. fn3(a.toOpenArray(1,3))
  16. when defined(js): discard # xxx bug #15952: `a` left unchanged
  17. else: doAssert a == [1, 0, 10, 20, 5]
  18. block: # bug #12521
  19. block:
  20. type slice[T] = openArray[T]
  21. # Proc using that alias
  22. proc testing(sl: slice[int]): seq[int] =
  23. for item in sl:
  24. result.add item
  25. let mySeq = @[1, 2, 3, 4, 5, 6, 7, 8, 9]
  26. doAssert testing(mySeq) == mySeq
  27. doAssert testing(mySeq[2..^2]) == mySeq[2..^2]
  28. block:
  29. type slice = openArray[int]
  30. # Proc using that alias
  31. proc testing(sl: slice): seq[int] =
  32. for item in sl:
  33. result.add item
  34. let mySeq = @[1, 2, 3, 4, 5, 6, 7, 8, 9]
  35. doAssert testing(mySeq) == mySeq
  36. doAssert testing(mySeq[2..^2]) == mySeq[2..^2]
  37. main()
  38. static: main()