Skip to content

mutable: fix wrong/misleading doc comments on Filter, FilterI, Map, MapI#888

Open
c-tonneslan wants to merge 1 commit into
samber:masterfrom
c-tonneslan:docs/mutable-filter-map-accurate
Open

mutable: fix wrong/misleading doc comments on Filter, FilterI, Map, MapI#888
c-tonneslan wants to merge 1 commit into
samber:masterfrom
c-tonneslan:docs/mutable-filter-map-accurate

Conversation

@c-tonneslan
Copy link
Copy Markdown

For #842.

A few of the doc comments in mutable/slice.go don't match what the code does:

  • Filter's doc says the predicate takes "an element of the slice and its index". It doesn't, only FilterI does.
  • Map and MapI claim "The function returns the modified slice, which has the same length as the original". Neither one returns anything.
  • Filter's "modifies the input slice in-place to contain only the elements that satisfy the predicate" is true of the returned slice header, but not of the caller's original variable, which keeps its original length and the tail is leftover from before the call (often duplicates of the last kept element). That's the surprise the reporter hit: [{foo} {baz} {baz}] instead of [{foo} {baz}].

Rewrote the comments to say what each function actually does and to spell out the leftover-tail behavior on Filter. No code change, so no new tests.

A few things in mutable/slice.go don't match the code:

- Filter's doc says the predicate takes "an element of the slice and its
  index". It doesn't, only FilterI does. That belongs on FilterI.
- Map's doc says "The function returns the modified slice, which has
  the same length as the original". It doesn't return anything. Same
  for MapI.
- Filter's "modifies the input slice in-place to contain only the
  elements that satisfy the predicate" is true of the returned slice
  header but not of the caller's original variable, which keeps its
  original length and may have leftover duplicates past the kept
  prefix. That's exactly what tripped the reporter in samber#842.

Rewrite the comments to say what each function actually does and to
spell out the leftover-tail gotcha on Filter. No code change.

For samber#842

Signed-off-by: Charlie Tonneslan <cst0520@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant