Skip to content

Commit 65a33c4

Browse files
author
maechler
committed
allow length(<expr>) <- n
git-svn-id: https://svn.r-project.org/R/trunk@88410 00db46b3-68df-0310-9c12-caf00c1e9a41
1 parent 1a2857b commit 65a33c4

File tree

4 files changed

+22
-7
lines changed

4 files changed

+22
-7
lines changed

doc/NEWS.Rd

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,11 @@
176176
\item \code{tools::checkReplaceFuns()} now deals better with
177177
replacement methods \emph{not} available as regular functions in the
178178
namespace.
179+
180+
\item \code{length(xpr) <- n} now also works for \code{expression}
181+
vectors, the same as for \code{list}s, i.e., padding with \code{NULL}
182+
entries if the length is increased. Thanks to \I{Mikael Jagan}'s
183+
\PR{18917}.
179184
}
180185
}
181186
}

src/library/base/man/length.Rd

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
% File src/library/base/man/length.Rd
22
% Part of the R package, https://www.R-project.org
3-
% Copyright 1995-2017 R Core Team
3+
% Copyright 1995-2025 R Core Team
44
% Distributed under GPL 2 or later
55

66
\name{length}
@@ -27,8 +27,11 @@ length(x) <- value
2727

2828
The replacement form can be used to reset the length of a vector. If
2929
a vector is shortened, extra values are discarded and when a vector is
30-
lengthened, it is padded out to its new length with \code{\link{NA}}s
31-
(\code{nul} for raw vectors).
30+
lengthened, it is padded out to its new length with \code{\link{NA}}s for
31+
atomic vectors but by \code{nul}, i.e., \code{as.raw(0)} for
32+
\code{\link{raw}} vectors, whereas \code{\link{list}} and
33+
\code{\link{expression}} vectors use \code{\link{NULL}}, the latter three
34+
cases in line with vector creation by \code{\link{vector}("<mode>", length)}.
3235

3336
Both are \link{primitive} functions.
3437
}
@@ -38,7 +41,8 @@ length(x) <- value
3841
\code{\link{integer}} of length 1, except for vectors of more than
3942
\eqn{2^{31}-1}{2^31 - 1} elements, when it returns a double.
4043

41-
For vectors (including lists) and factors the length is the number of
44+
For vectors (including lists and expressions) and factors the length is
45+
the number of
4246
elements. For an environment it is the number of objects in the
4347
environment, and \code{NULL} has length 0. For expressions and
4448
pairlists (including \link{language objects} and dot-dot-dot lists) it is the

src/main/builtin.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,7 @@ SEXP xlengthgets(SEXP x, R_xlen_t len)
904904
}
905905
break;
906906
case VECSXP:
907+
case EXPRSXP:
907908
for (i = 0; i < len; i++)
908909
if (i < lenx) {
909910
SET_VECTOR_ELT(rval, i, VECTOR_ELT(x, i));
@@ -940,12 +941,10 @@ SEXP lengthgets(SEXP x, R_len_t len)
940941

941942
attribute_hidden SEXP do_lengthgets(SEXP call, SEXP op, SEXP args, SEXP rho)
942943
{
943-
SEXP x, ans;
944-
945944
checkArity(op, args);
946945
check1arg(args, call, "x");
947946

948-
x = CAR(args);
947+
SEXP ans, x = CAR(args);
949948

950949
/* DispatchOrEval internal generic: length<- */
951950
if(isObject(x) && DispatchOrEval(call, op, "length<-", args,

tests/reg-tests-1e.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2021,6 +2021,13 @@ stopifnot(ch %in% D, D %in% ch, !(c2 %in% D), !(D %in% c2))
20212021
## had failed in R-devel around 2025-06-26 (and before R 4.3.0)
20222022

20232023

2024+
## length(<expression>) <- value
2025+
x <- expression(.)
2026+
length(x) <- 0L
2027+
stopifnot(identical(x, expression()),
2028+
identical(`length<-`(x, 2L), expression(NULL, NULL)))
2029+
2030+
20242031

20252032
## keep at end
20262033
rbind(last = proc.time() - .pt,

0 commit comments

Comments
 (0)