flatty icon indicating copy to clipboard operation
flatty copied to clipboard

Flatty doesnt handle custom range arrays properly

Open beef331 opened this issue 3 years ago • 1 comments

These are just some examples, but it extends to any array that does not start at 0. Depending on the array and if 0 can be converted to the index type, they will have a bug at runtime or just will not compile.

import flatty

var 
  arr: array[-2..0, int]
  arr2: array[10..11, int]
for i, x in arr.pairs:
  arr[i] = i
for i, x in arr2.pairs:
  arr2[i] = i

assert arr2.toFlatty().fromFlatty(array[10..11, int]) == [10, 11] # Compile time error
assert arr.toFlatty().fromFlatty(array[-2..0, int]) == [-2, -1, 0] # Runtime bug

Perhaps instead of your present api you may want to consider

proc toFlatty*[T](s: var string, x: openarray[T]) =
  when T.supportsMemCopy:
    # Copy to
  else:
    for val in x:
      s.toFlatty(val)
  
proc toFlatty*[N, T](s: var string, x: array[N, T]) =
  s.toFlatty(x.toOpenArray(0, x.len - 1))
  
proc toFlatty*[T](s: var string, x: seq[T]) =
  if x.len > 0:
    s.toFlatty x.len
    s.toFlatty(x.toOpenArray(0, x.len - 1))

beef331 avatar Aug 30 '22 20:08 beef331

I spent several hours to make ranges work, but I just get very strange and puzzling errors.

I think its some sort of compiler bugs that prevent ranges from working.

treeform avatar Jan 18 '24 06:01 treeform