moodymudskipper / inops

Infix Operators for Detection, Subsetting and Replacement
GNU General Public License v3.0
40 stars 0 forks source link

The following object is masked from ‘package:base’: <<- #41

Closed moodymudskipper closed 4 years ago

moodymudskipper commented 4 years ago

When attaching our package, the following text is displayed :

library(inops)

Attaching package: ‘inops’

The following object is masked from ‘package:base’:

<<-

This happens because our package indeed redefines this operator, however no need to worry, it doesn't affect in any way any base or packaged code (these won't ever "see" inops::<<-).

Moreover, if you choose to use <<- in its binary form x <<- y you will not find any difference, this operator has been redefined so the syntax x < y <- value can be supported. Indeed just like class(x) <- value calls the function class<- and x == y <- value calls the function ==<-, calling x < y <- value calls <<-. You can actually try it in a session where inops is not attached :

x <- 1
y <- 2
x < y <- 3
#> Error in x < y <- 3: incorrect number of arguments to "<<-"

Created on 2019-11-22 by the reprex package (v0.3.0)

Our package simply implements the 3 parameter usage and leaves the original binary usage unaffected.

Now for those worried about the performance cost, here is a benchmark, where I ran each instruction a million times :

bench::mark(iterations= 10^6,
  base::`<<-`(x,1),
  inops::`<<-`(x,1)
)
#> # A tibble: 2 x 6
#>   expression              min   median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr>         <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#> 1 base::`<<-`(x, 1)     3.7us    4.3us   192856.        0B     22.0
#> 2 inops::`<<-`(x, 1)    7.4us    8.3us   110235.    34.8KB     24.6

Created on 2019-11-22 by the reprex package (v0.3.0)

inops::`<<-` has an overhead of 4us. This means that if you call it one million time it'll take 8 seconds rather than 4.

Given that this overhead will disappear as soon as you package your function, and that most experienced users will recommend you to stay as far away of this function as possible, we believe worries aren't necessary.

Note that we won't ever try to dissimulate this warning, but might find a way to make it more understandable by all.