S7 and a new `:=` named-bind operator

Open
#7,801 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
30/100
Issue type
Feature
Clarity
Needs clarification
Activity status
Quiet
Tech stack
r

Research direction

Start with the existing := behavior in data.table and compare it with the S7 package's proposed named-bind operator and rlang's definition. No files or tests are identified; done would require an agreed compatibility approach and a clearly specified implementation or feedback outcome.

Written by the indexing model from the issue text.

Description

The dev version of the S7 package introduces a new := operator:

`:=` <- function(lhs, rhs) {
  cl <- sys.call()
  if (!is.symbol(cl[[2L]])) {
    stop2("Left-hand side of `:=` must be a symbol.")
  }
  if (!is.call(cl[[3L]])) {
    stop2("Right-hand side of `:=` must be a function call.")
  }
  if ("name" %in% names(cl[[3L]])) {
    stop2("Right-hand side of `:=` must not already supply a `name` argument.")
  }

  cl[[1L]] <- quote(`<-`)
  cl[[3L]]$name <- as.character(cl[[2L]])

  # Can't use eval because it introduces a new frame
  invisible(.Call(S7_eval_bare_, cl, parent.frame()))
}

This is designed to support creating new classes and generics (and various more exotic objects) where you want to give an object a name and bind it to that name.

# with <-
foo <- new_class("foo")
bar <- new_generic("bar")

# with :=
foo := new_class()
bar := new_generic()

This feels like a useful and general tool and @lawremi thinks there's some chance it will make it to base R.

The downside is that it conflicts with the existing definitions of := in data.table and rlang which both error to suggest to the user they've done something wrong. (It doesn't actually affect the operation of these packages since both data.table and rlang override the meaning of := via NSE.)

We don't have any solutions to this problem yet (although we're experimenting with a .onLoad hook approach for rlang that we can share when it's done), but I wanted to give y'all a heads up that we're seriously exploring it and give you an opportunity to give feedback to the S7 team.

(cc @t-kalinowski)

Dominant language
R
Stars
3.9k
Forks
1.1k
Avg merge
14h 4m
Merged PRs (30d)
4

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Rdatatable/data.table

All issues in Rdatatable/data.table

Similar issues

More R issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.