Skip to content

Inconsistent use of the Index argument in treelist #1471

@yiyunliu

Description

@yiyunliu

Currently, both treelist-ref and treelist-set take an index of type Index instead of Integer, which is not only inconsistent with how other collections handle indices (e.g. string-ref, vector-ref, and vector-set! all take an Integer as their argument to index into the container), but also makes it difficult to do arithmetic on indices.

#lang typed/racket
(require racket/treelist)

(: idx Index)
(define idx 0)

(: tr (TreeListof String))
(define tr (treelist "apple" "orange"))

;; okay
(treelist-ref tr idx)
;; not okay, (add1 idx) is no longer an Index
(treelist-ref tr (add1 idx))
;; okay, but annoying
(let ([new-idx (add1 idx)])
  (if (index? new-idx)
      (treelist-ref tr new-idx)
      (error "not an index")))

The same issue doesn't apply if those functions took Integer as inputs instead.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions