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 implements _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 }

