HenrikBengtsson / Wishlist-for-R

Features and tweaks to R that I and others would love to see - feel free to add yours!
https://github.com/HenrikBengtsson/Wishlist-for-R/issues
GNU Lesser General Public License v3.0
134 stars 4 forks source link

Support for decorators #131

Open lorenzwalthert opened 2 years ago

lorenzwalthert commented 2 years ago

For example like in Python. This would allow things like

# cache a function
@R.cache::cache()
slow_fun <- function() {
  ...
}

# parametrize tests with all combinations like pytetst
@testthat::cross(x = 1:2, y = 1:2) 
test_that('can create prod', {
  expect_equal(
    my_prod(x, y), 
    x * y
  )
})
nfultz commented 2 years ago

couple thoughts

  1. the at sign is already taken for S4
  2. In python, it only works with def and not lambda. R's function works closer to lambda.
  3. You can already write "setter" functions eg how names()<- and class()<- work. They are not exactly the same, but could be used to do similar things.
lorenzwalthert commented 2 years ago

Thanks @nfultz. I know it's already taken for S4, but I think it would be still possible, since with S4, it's always within an expression as far as I know, e.g. x@y, and never @y, e.g. as the first token on a line. Right? There are symbols in R that have multiple meanings depending on the context, e.g. + as an infix operator for two arguments or as a sign.

ColinFay commented 2 years ago

Would you be ok with explaining this to someone not knowing Python and that doesn't know what decorators are? :)

nfultz commented 2 years ago

I think @ is a primitive so it's a special case. I guess if - can have a different meaning in prefix and infix form, there's no reason @ couldn't.

Another thought - R generally doesn't use significant whitespace like Python does, so not clear to me how it could know to apply a decorator to the next line.

@Colin Fay - decorators are syntax sugar for applying higher order functions. so instead of writing

a <- function(x) { ...}

a <- cache(a)

one would write

@cache
a <- function(x) { ... }

On Fri, Jan 28, 2022 at 8:29 AM Lorenz Walthert @.***> wrote:

Thanks @nfultz https://github.com/nfultz. I know it's already taken for S4, but I think it would be still possible, since with S4, it's always within an expression as far as I know, e.g. @.***, and never @y, e.g. as the first token on a line. Right?

— Reply to this email directly, view it on GitHub https://github.com/HenrikBengtsson/Wishlist-for-R/issues/131#issuecomment-1024383232, or unsubscribe https://github.com/notifications/unsubscribe-auth/AADGGTWZHDYT5CHEAK5MM2LUYK777ANCNFSM5NBFHTBA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you were mentioned.Message ID: @.***>

HenrikBengtsson commented 2 years ago

Potential related to this, is https://github.com/dirkschumacher/defmacro. It might provide a way to declare these this using R syntax.