Skip to content

Flatty doesnt handle custom range arrays properly #32

Closed

Description

@beef331

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))

Activity

  1. treeform commented on Jan 18, 2024

    @treeform
    Owner

    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.

  2. treeform commented on May 25, 2026

    @treeform
    Owner

    Thank you for your issue. I have been working on flatty for a bunch of things, and I think I have fixed this in this PR: #52

    Feel free to re-open the issue if it still happens to you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions