force                  package:base                  R Documentation

_F_o_r_c_e _E_v_a_l_u_a_t_i_o_n _o_f _a_n _A_r_g_u_m_e_n_t

_D_e_s_c_r_i_p_t_i_o_n:

     Forces the evaluation of a function argument.

_U_s_a_g_e:

     force(x)

_A_r_g_u_m_e_n_t_s:

       x: a formal argument of the enclosing function.

_D_e_t_a_i_l_s:

     'force' forces the evaluation of a formal argument.  This can be
     useful if the argument will be captured in a closure by the
     lexical scoping rules and will later be altered by an explicit
     assignment or an implicit assignment in a loop or an apply
     function.

_N_o_t_e:

     This is semantic sugar: just evaluating the symbol will do the
     same thing (see the examples).

     'force' does not force the evaluation of other promises.  (It
     works by forcing the promise that is created when the actual
     arguments of a call are matched to the formal arguments of a
     closure, the mechanism which implememnts _lazy evaluation_.)

_E_x_a_m_p_l_e_s:

     f <- function(y) function() y
     lf <- vector("list", 5)
     for (i in seq(along = lf)) lf[[i]] <- f(i)
     lf[[1]]()  # returns 5

     g <- function(y) { force(y); function() y }
     lg <- vector("list", 5)
     for (i in seq(along = lg)) lg[[i]] <- g(i)
     lg[[1]]()  # returns 1

     ## This is identical to
     g <- function(y) { y; function() y }

