Skip to content

Strict pluck #482

Closed
Closed

Description

@hadley

In base R, [[ sometimes returns an error and sometimes returns NULL:

row[[col]] Zero-length OOB (int) OOB (chr) Missing
NULL NULL NULL NULL NULL
Atomic Error Error Error Error
List Error Error NULL NULL

pluck() eliminates this inconsistently by (effectively) always return NULL. We need a strict equivalent that eliminates the inconsistency in the opposite direction: by always throwing an error.

Activity

  1. hadley commented on Mar 29, 2018

    @hadley
    MemberAuthor

    We could call it chuck() or suck()

  2. daniel-barnett commented on May 3, 2018

    @daniel-barnett
    Contributor

    Were you thinking something like the following (but without the strict parameter)? If so, I can clean it up and make a PR.

    test.list <- list(a = 1:5, b = letters[1:5], c = list(abc = 123))
    test.atom <- 1:10
    
    > # strict_pluck()
    > pluck(NULL, integer(0), strict = TRUE)
    Error: `.x` must not be NULL.
    > pluck(NULL, 100, strict = TRUE)
    Error: `.x` must not be NULL.
    > pluck(NULL, "adsaf", strict = TRUE)
    Error: `.x` must not be NULL.
    > pluck(NULL, NA_integer_, strict = TRUE)
    Error: `.x` must not be NULL.
    
    > pluck(test.list, integer(0), strict = TRUE)
    Error: Can't find index.
    > pluck(test.list, 100, strict = TRUE)
    Error: Can't find index.
    > pluck(test.list, "adsaf", strict = TRUE)
    Error: Can't find index.
    > pluck(test.list, NA_integer_, strict = TRUE)
    Error: Can't find index.
    
    > pluck(test.atom, integer(0), strict = TRUE)
    Error: Can't find index.
    > pluck(test.atom, 100, strict = TRUE)
    Error: Can't find index.
    > pluck(test.atom, "adsaf", strict = TRUE)
    Error: Can't find index.
    > pluck(test.atom, NA_integer_, strict = TRUE)
    Error: Can't find index.
  3. hadley commented on May 3, 2018

    @hadley
    MemberAuthor

    Exactly!

  4. hadley commented on May 3, 2018

    @hadley
    MemberAuthor

    A PR would be great 😄

  5. daniel-barnett commented on May 10, 2018

    @daniel-barnett
    Contributor

    So, I also have this working for environments and S4 objects. For environments, it errors if the object doesn't exist in the environment, and for S4 if the object doesn't have the desired slot. This would the desired behaviour, right?

    The other big one is indexing by function, but (correct me if I'm wrong) I don't think we can really make this strict as it relies on whatever that function's behaviour is.

  6. hadley commented on May 10, 2018

    @hadley
    MemberAuthor

    That all sounds correct to me

  7. added a commit that references this issue on Nov 27, 2018
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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions