jonocarroll / vec

A New Vector Class With Added Functionality
https://jonocarroll.github.io/vec/
GNU General Public License v3.0
6 stars 0 forks source link

Ignore trailing commas #3

Open jonocarroll opened 6 days ago

jonocarroll commented 6 days ago

One suggestion, it could be great to be able to set:

as_vec( a, b, )

Just like rlang::list2 does using ... param.

Originally posted by @jrosell in https://github.com/jonocarroll/vec/issues/1#issuecomment-2402607650

jonocarroll commented 6 days ago

I see the advantage for sure. One complication of taking the 'splatted' arguments instead of a single argument is that they need to be combined into a vector; for a list that isn't a problem

rlang::list2(1, "2", 3i, )
[[1]]
[1] 1

[[2]]
[1] "2"

[[3]]
[1] 0+3i

but for a vector I'd need to call c(...) inside as_vec() which would "hide" the coercion

c(1, "2", 3i)
[1] "1"    "2"    "0+3i"

On the other hand, someone calling as_vec(c(a, b, c)) isn't going to "see" the coercion either.

{rlang} does provide vector constructors that use dynamic-dots, but it seems to error on coercion

rlang::chr(1, "2", 3i, )
Error:
! Can't convert a double vector to a character vector.

rlang::chr("1", "2", "3i", )
[1] "1"  "2"  "3i"

which may be why there's not a generalised version.

I'll keep thinking on this - perhaps there's a way.

jrosell commented 6 days ago

Could an optional FUN.VALUE (like vapply) or cast_to argument make sense in this case?

Then maybe use: https://vctrs.r-lib.org/reference/vec_cast.html