Home > Software engineering >  How does {rlang}'s 'curly-curly' operator `{{` work?
How does {rlang}'s 'curly-curly' operator `{{` work?

Time:10-06

The {rlang} documentation at help("nse-force") gives the following:

The curly-curly operator {{ }} for function arguments is a bit special because it forces the function argument and immediately defuses it. The defused expression is substituted in place, ready to be evaluated in another context, such as the data frame.

I'm similarly confused about the 'bang-bang' operator !!, about which the documentation is just as obtuse regarding what's going on under-the-hood.

My question isn't about how to use the operator as its usage is (I think) quite straightforward. Instead I'd like to know how such an operator is actually implemented in {rlang} behind the scenes. According to one of the package authors, {{ foo }} basically becomes !!rlang::enquo(foo). However I'm still at a loss to understand how non-standard operators like this are actually implemented, especially considering that this one seems to 'just work', regardless of whether it's being used by {rlang} functions.

Looking at the source code all I can guess is that it's being done in C or C . Can anyone give me some more information?

CodePudding user response:

However I'm still at a loss to understand how non-standard operators like this are actually implemented, especially considering that this one seems to 'just work', regardless of whether it's being used by {rlang} functions.

It doesn’t “just work” with arbitrary functions — on the contrary: functions do need to be aware of tidy evaluation. And as you probably guessed there’s no {{ operator. Instead, ‘rlang’ uses NSE to capture the unevaluated argument and then checks whether the parse tree of the expression contains two nested { calls. It then takes the unevaluated expression and transforms it appropriately.

  • Related