Skip to content

Documented side effects of := in repeated execution#7786

Open
venom1204 wants to merge 4 commits into
masterfrom
issue7409
Open

Documented side effects of := in repeated execution#7786
venom1204 wants to merge 4 commits into
masterfrom
issue7409

Conversation

@venom1204

Copy link
Copy Markdown
Contributor

closes #7409

Documented := side effects in repeated execution across datatable-reference-semantics.Rmd, assign.Rd, and test.Rd.

hi @ben-schwen, whenever you have a chance, could you please take a look at this PR.

thanks

@venom1204 venom1204 requested a review from joshhwuu June 8, 2026 12:09
@venom1204 venom1204 requested a review from MichaelChirico as a code owner June 8, 2026 12:09
@venom1204 venom1204 requested a review from tdhock June 8, 2026 18:05

### d) Side effects and testing

Because `:=` modifies by reference, changes are cumulative. If the same *data.table* is reused—for example, in a loop or when using `test()` with multiple `optimization` levels—subsequent runs will start with the modified table from the previous run. Use `copy()` to ensure each run starts with the same data.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

levels — subsequent ?

Comment thread man/assign.Rd Outdated
\code{DT[a > 4, b := c]} is different from \code{DT[a > 4][, b := c]}. The first expression updates (or adds) column \code{b} with the value \code{c} on those rows where \code{a > 4} evaluates to \code{TRUE}. \code{X} is updated \emph{by reference}, therefore no assignment needed. Note that this does not apply when \code{i} is missing, i.e. \code{DT[]}.

The second expression on the other hand updates a \emph{new} \code{data.table} that's returned by the subset operation. Since the subsetted data.table is ephemeral (it is not assigned to a symbol), the result would be lost; unless the result is assigned, for example, as follows: \code{ans <- DT[a > 4][, b := c]}.
Note that \samp{:=} modifications are cumulative. When reusing a \code{data.table} in loops or multi-level tests, use \code{\link{copy}} to ensure a fresh state.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think cumulative is good word here. In-place changing it's input seems to be more precise.

@venom1204 venom1204 requested review from jangorecki and joshhwuu June 10, 2026 08:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Document side effects of := on keys

3 participants