Your version using sequences works well. I wondered how an array version of
that would look, so tried using your sequence version but with a ref array, ie:
***************
*** 12,15 ****
proc main() =
! var a = newSeq[E](els)
!
for i in 0..high(a):
--- 12,17 ----
proc main() =
! var
! a: ref array[els, E]
!
! new a
for i in 0..high(a):
ms:nim jim$ nim c sort4
/Users/jim/nim/sort4.nim(17, 19) Error: type mismatch: got <ref
array[0..9999999, E]>
but expected one of:
proc high(T: typedesc[SomeFloat]): T:type
first type mismatch at position: 1
required type for T: type SomeFloat
but expression 'a' is of type: ref array[0..9999999, E]
proc high(x: cstring): int
first type mismatch at position: 1
required type for x: cstring
but expression 'a' is of type: ref array[0..9999999, E]
proc high(x: string): int
first type mismatch at position: 1
required type for x: string
but expression 'a' is of type: ref array[0..9999999, E]
proc high[I, T](x: array[I, T]): I
first type mismatch at position: 1
required type for x: array[I, T]
but expression 'a' is of type: ref array[0..9999999, E]
proc high[I, T](x: typedesc[array[I, T]]): I
first type mismatch at position: 1
required type for x: type array[I, T]
but expression 'a' is of type: ref array[0..9999999, E]
proc high[T: Ordinal | enum | range](x: T): T
first type mismatch at position: 1
required type for x: T: Ordinal or enum or range
but expression 'a' is of type: ref array[0..9999999, E]
proc high[T: Ordinal | enum | range](x: typedesc[T]): T
first type mismatch at position: 1
required type for x: type T: Ordinal or enum or range
but expression 'a' is of type: ref array[0..9999999, E]
proc high[T](x: openArray[T]): int
first type mismatch at position: 1
required type for x: openArray[T]
but expression 'a' is of type: ref array[0..9999999, E]
expression: high(a)
Run
I don't understand why high(a) works if the array isn't a ref array, but fails
on a ref array. But to continue, I changed high(a) to ..< els. Then it didn't
like sort:
/Users/jim/nim/sort4a.nim(20, 4) Error: type mismatch: got <ref
array[0..9999999, E]>
but expected one of:
func sort[T](a: var openArray[T]; cmp: proc (x, y: T): int {.closure.};
order = SortOrder.Ascending)
first type mismatch at position: 1
required type for a: var openArray[T]
but expression 'a' is of type: ref array[0..9999999, E]
proc sort[T](a: var openArray[T]; order = SortOrder.Ascending)
first type mismatch at position: 1
required type for a: var openArray[T]
but expression 'a' is of type: ref array[0..9999999, E]
expression: sort(a)
Run
Shouldn't this change from array to ref array + new have worked?