Skip to content

deprecate API's with string/seq + start/last in favor of ones with openArray #381

Open
@timotheecour

Description

@timotheecour

proposal

benefits

  • simpler APIs (less overloads needed) and separation of concern (callee doesn't need to care where the slice came from), less code bloat
  • no broken edge cases (see example 2 below), no design by convention
  • more flexible

example 1

proc findBounds*(input: openArray[char], pattern: Regex, matches: var openArray[Slice[int]],
  start = 0): tuple[Slice[int]]

which would subsume all others and also be strictly more flexible, and less error prone

refs nim-lang/Nim#18028 (comment)

example 2

(refs nim-lang/Nim#18173)
optional start/last parameters lead to bad API's where a valid value ends up hijacked with a different meaning, eg:

func find*(a: SkipTable, s, sub: string, start: Natural = 0, last = 0): int

here, when user specifies last = 0, it ends up meaning last = sub.high, so there's no way for user to specify the edge condition of last = 0, which should be a valid choice

changing the signature to func find*(a: SkipTable, s, sub: string, start: Natural = 0, last = -1): int would be an improvement but is still unsatisfactory, as this could equally be interpreted as meaning s.high or empty slice of s (EDIT: func find*(a: SkipTable, s, sub: string, start: Natural = 0, last = sub.high): int would be possible too)

instead, func find*(a: SkipTable, s: openArray[char], sub: string): int would be self-documenting and not have any edge cases, the user then would call simply toOpenArray(s, start, end) if they want a slice

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