Skip to content

Feature request: Add get_or_compute/compute_if_absent function #401

@yyny

Description

@yyny

Inspired by computeIfAbsent from Java;

  • Search for a value by key.
  • If no value is found, compute a value and insert it.
  • Return a reference to the value.

For Sets, the expected behavior when they key is not found is to insert into a separate entry, rather than populating the entry associated with the key.

This would provide an easy and efficient path for people looking for this behavior, and avoid misuse of get_or_insert_with, which appears to do the same thing, but doesn't, see #399 and the discussion in #396.

I propose the name get_or_compute rather than following Java, since it makes it harder to accidentally use get_or_insert_with because autocomplete will suggest both.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions