Skip to content

Commit 3933ee8

Browse files
friendlier error in assignment with trailing comma
e.g. `DT[, `:=`(a = 1, b = 2,)`. WIP. Need to add tests and such, but editing from browser before I forget.
1 parent c4a2085 commit 3933ee8

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

R/data.table.R

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1127,7 +1127,16 @@ replace_dot_alias = function(e) {
11271127
} else {
11281128
# `:=`(c2=1L,c3=2L,...)
11291129
lhs = names(jsub)[-1L]
1130-
if (any(lhs=="")) stopf("In %s(col1=val1, col2=val2, ...) form, all arguments must be named.", if (root == "let") "let" else "`:=`")
1130+
if (!all(nzchar(lhs))) {
1131+
# friendly error for common case: trailing terminal comma
1132+
n_lhs = length(lhs)
1133+
# TODO(michaelchirico): use missing instead
1134+
if (lhs[n_lhs] == "" && all(nzchar(lhs[-n_lhs]))) {
1135+
stopf("In %s(col1=val1, col2=val2, ...) form, all arguments must be named, but the last argument has no name. Did you forget a trailing comma?", if (root == "let") "let" else "`:=`")
1136+
} else {
1137+
stopf("In %s(col1=val1, col2=val2, ...) form, all arguments must be named.", if (root == "let") "let" else "`:=`")
1138+
}
1139+
}
11311140
names(jsub)=""
11321141
jsub[[1L]]=as.name("list")
11331142
}

0 commit comments

Comments
 (0)