renkun-ken / pipeR

Multi-Paradigm Pipeline Implementation
Other
169 stars 39 forks source link

Two mechanisms to determine the function to call using $ with Pipe #40

Closed renkun-ken closed 10 years ago

renkun-ken commented 10 years ago

Consider the following example:

> library(pipeR)
> f <- function(x) cat("global f")
> Pipe(0)$f()
global f
> pf <- Pipe(0)$f
> pf()
global f
> local({f <- function(x) cat("local f"); pf()})
global f
> local({f <- function(x) cat("local f"); Pipe(0)$f()})
local f
> local({f <- function(x) cat("local f"); pf2 <<- Pipe(0)$f})
> pf2()
local f

Function is determined when $ is called, rather than when the function is called. This design avoids potential confusion if the local environment accidentally contains a function having the same name.

A more careful consideration should be done to determine which design is more reasonable: the static calling mechanism (which function to call is determined when $ is called), or the dynamic calling mechanism (which function to call is determined only when the function is being called).

renkun-ken commented 10 years ago

The non-lazy mechanism is definitely more reasonable and predictable.