What is a promise in R and what does force() do?

Quick Overview

Asks what a promise is in R and what the force() function does. Tests understanding of lazy evaluation, how argument values are cached after first use, and the closure bug in function factories that forcing an argument prevents.

What is a promise in R and what does force() do?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

What is a promise in R and what does `force()` do? ```hint When is an argument computed? Think about the moment R actually computes the value of an argument, and what a closure created inside a loop sees when it finally runs. ``` ### What a Strong Answer Covers - What a promise holds and when R evaluates it - How many times a promise is evaluated, and what that means for side effects and errors in an argument - What happens to an argument the function never uses - What `force()` does, and the function factory bug it prevents, shown with an example that really reproduces the bug ### Follow-up Questions - In which environment is a default argument evaluated, compared with an argument the caller supplied? - Why does `lapply(1:3, make_adder)` return correct adders in current R even without `force()`, while a `for` loop does not? - How do `substitute()` and `delayedAssign()` relate to promises?

Overview: Asks what a promise is in R and what the force() function does. Tests understanding of lazy evaluation, how argument values are cached after first use, and the closure bug in function factories that forcing an argument prevents.

General logo
General
Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

What is a promise in R and what does force() do?

What a Strong Answer Covers Guidance

  • What a promise holds and when R evaluates it
  • How many times a promise is evaluated, and what that means for side effects and errors in an argument
  • What happens to an argument the function never uses
  • What force() does, and the function factory bug it prevents, shown with an example that really reproduces the bug

Follow-up Questions Guidance

  • In which environment is a default argument evaluated, compared with an argument the caller supplied?
  • Why does lapply(1:3, make_adder) return correct adders in current R even without force() , while a for loop does not?
  • How do substitute() and delayedAssign() relate to promises?