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

WISH: attr() allows to provide a default fallback value #139

Open
zeehio opened this issue Sep 15, 2022 · 4 comments
Open

WISH: attr() allows to provide a default fallback value #139

zeehio opened this issue Sep 15, 2022 · 4 comments

Comments

@zeehio
Copy link

zeehio commented Sep 15, 2022

Wish

attr() allows to specify default=value to return a default value if the requested attribute is not found.

x <- 1

# current:
w <- attr(x, "hello")
if (is.null(w)) {
  w <- "world"
}

# wish:
w <- attr(x, "hello", default="world")

PS: big fan of your work. Thanks for this repository and for all your packages! 😃

@HenrikBengtsson
Copy link
Owner

HenrikBengtsson commented Sep 15, 2022

I like this idea - this addition to attr() would be consistent to how getOption() and Sys.getenv() provide "default" fallbacks. Not sure if there are other functions too with this too.

FWIW, I wasn't sure whether NULL was a valid attribute value. It turns out it's not; it's not possible to have attributes will value NULL;

> x <- 42L; attributes(x) <- alist(abc = 1, def = NULL, ghi = 3)
> str(attributes(x))
List of 2
 $ abc: num 1
 $ ghi: num 3

In other words, one does not have to account for that as a special case and your "current" snippet is sufficient.

PS: big fan of your work. Thanks for this repository and for all your packages! smiley

Thanks. Happy to hear :)

@nfultz
Copy link

nfultz commented Sep 15, 2022

Might want to disable partial matching when a default is provided?

@vh-d
Copy link

vh-d commented Sep 15, 2022

I prefer more generic approach like this in my code

w <- ifnull(attr(x, "hello"), "world")

Would be nice to have something like this in base but the addition would potentially break someone's code of course.

@HenrikBengtsson
Copy link
Owner

Might want to disable partial matching when a default is provided?

Good point.

Though, I can see how "magically" setting exact = TRUE when default is specified, can become a point of confusion and possibly result in a blocking discussion for implementing support for a new argument default. If exact = TRUE becomes the new default, which I hope it one day will, this would be moot.

An alternative to having the default value of exact depend on whether argument default i specified, is to require exact = TRUE whenever default is specified. This means you'd had to write:

w <- attr(x, "hello", exact = TRUE, default = "world")

for now, to avoid an error. This would also help to prepare for the day when exact = TRUE becomes the new default (I hope).

(Just my thoughts)

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

No branches or pull requests

4 participants