Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

strict_list

Strict lists


Description

A strict list is essentially a normal list() but it does not allow partial matching with $.

Usage

strict_list(...)

as_strict_list(x)

## S3 method for class 'xfun_strict_list'
x$name

## S3 method for class 'xfun_strict_list'
print(x, ...)

Arguments

...

Objects (list elements), possibly named. Ignored in the print() method.

x

For as_strict_list(), the object to be coerced to a strict list.

For print(), a strict list.

name

The name (a character string) of the list element.

Details

To me, partial matching is often more annoying and surprising than convenient. It can lead to bugs that are very hard to discover, and I have been bitten by it many times. When I write x$name, I always mean precisely name. You should use a modern code editor to autocomplete the name if it is too long to type, instead of using partial names.

Value

Both strict_list() and as_strict_list() return a list with the class xfun_strict_list. Whereas as_strict_list() attempts to coerce its argument x to a list if necessary, strict_list() just wraps its argument ... in a list, i.e., it will add another list level regardless if ... already is of type list.

Examples

library(xfun)
(z = strict_list(aaa = "I am aaa", b = 1:5))
z$a  # NULL!
z$aaa  # I am aaa
z$b
z$c = "create a new element"

z2 = unclass(z)  # a normal list
z2$a  # partial matching

z3 = as_strict_list(z2)  # a strict list again
z3$a  # NULL again!

xfun

Miscellaneous Functions to Support Packages Maintained by 'Yihui Xie'

v0.22
MIT + file LICENSE
Authors
Yihui Xie [aut, cre, cph] (<https://orcid.org/0000-0003-0645-5666>), Wush Wu [ctb], Daijiang Li [ctb], Xianying Tan [ctb], Salim Brüggemann [ctb] (<https://orcid.org/0000-0002-5329-5987>), Christophe Dervieux [ctb]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.