New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strict pluck #482

Closed
hadley opened this Issue Mar 29, 2018 · 6 comments

Comments

Projects
None yet
2 participants
@hadley
Copy link
Member

hadley commented Mar 29, 2018

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.

@hadley

This comment has been minimized.

Copy link
Member

hadley commented Mar 29, 2018

We could call it chuck() or suck()

@daniel-barnett

This comment has been minimized.

Copy link
Contributor

daniel-barnett commented May 3, 2018

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.
@hadley

This comment has been minimized.

Copy link
Member

hadley commented May 3, 2018

Exactly!

@hadley

This comment has been minimized.

Copy link
Member

hadley commented May 3, 2018

A PR would be great 😄

@daniel-barnett

This comment has been minimized.

Copy link
Contributor

daniel-barnett commented May 10, 2018

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.

@hadley

This comment has been minimized.

Copy link
Member

hadley commented May 10, 2018

That all sounds correct to me

@lionel- lionel- closed this in 94572ed Nov 27, 2018

lionel- added a commit that referenced this issue Nov 27, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment