10.5 Fundamentals - Forcing

  • Lazy evaluation means arguments only evaluated when used
  • “[can] lead to a real head-scratcher of a bug”
x <- 2
square <- power1(x)
x <- 3
square(4)
#> [1] 64
  • Only applies if passing object as argument
  • Here argument 2 evaluated when function called
square <- power1(2)
x <- 3
square(4)
#> [1] 16

So use force()! (Unless you want it to change with the x in the parent environment)