Skip to content
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
Closed

Strict pluck #482

hadley opened this issue Mar 29, 2018 · 6 comments
Labels
feature a feature request or enhancement pluck 🍐

Comments

@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
Copy link
Member Author

hadley commented Mar 29, 2018

We could call it chuck() or suck()

@daniel-barnett
Copy link
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.

@hadley
Copy link
Member Author

hadley commented May 3, 2018

Exactly!

@hadley
Copy link
Member Author

hadley commented May 3, 2018

A PR would be great 😄

@hadley hadley added feature a feature request or enhancement pluck 🍐 labels May 5, 2018
@daniel-barnett
Copy link
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.

@hadley
Copy link
Member Author

hadley commented May 10, 2018

That all sounds correct to me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement pluck 🍐
Projects
None yet
Development

No branches or pull requests

2 participants