metaprogramming - Implement ifLet function in R -
here code:
setgeneric("iflet", function(sym, x, body1, body2, ...) { standardgeneric("iflet") }) setmethod("iflet", signature(sym = "name", x = "any", body1 = "any", body2 = "any"), function(sym, x, body1, body2 = {}) { e = new.env() sym_str = deparse(substitute(sym)) iflet(sym_str, x, body1, body2) }) setmethod("iflet", signature(sym = "name", x = "any", body1 = "any", body2 = "any"), function(sym, x, body1, body2 = {}) { stopifnot(length(sym) == 1) e = new.env() assign(sym, x, envir = e) if(e[[sym]]) { eval(substitute(body1), e) } else { eval(substitute(body2), e) } }) iflet("..temp..", true, {print(paste("true.", as.character(..temp..)))}, {print(paste("false.", as.character(..temp..)))}) iflet(..temp.., true, {print(paste("true.", as.character(..temp..)))}, {print(paste("false.", as.character(..temp..)))})
the "..temp.." version works fine, ..temp..
version produces error:
error in evaluating argument 'sym' in selecting method function 'iflet': error: object '..temp..' not found
why error come out , how can solve this?
Comments
Post a Comment