I was surprised to find out that vec_math() does not have an implementation for integer types. This means, if implementing an S3 rcrd class which wraps around integers, we cannot implement a pass-down math generic as (for example)
In the default implementation, data gets passed down to vec_math_base() which calls the function from base, and then the output is restored to the input type. I cannot see an issue from vec_math_base() if we allow integer (or even complex) arguments through, but the Math operations on integers do not necessarily return integers (e.g. log(2L) and mean(1:10) both return something that cannot be restored to integer type).
Would there be concerns with removing the vec_restore() from vec_math.default() and allowing all numeric types through?
Finally, I've started using vctrs hands-on to build a class, and it's a great package and I appreciate all the work on it!
I was surprised to find out that
vec_math()
does not have an implementation for integer types. This means, if implementing an S3rcrd
class which wraps around integers, we cannot implement a pass-down math generic as (for example)Reproducible example
Actual behaviour:
What I would expect: Identical to
sum()
on integers, which converts to double output.Workaround if required:
sessionInfo()
output (checked on 0.6.5, and this reprex from latest master):Created on 2024-08-10 with reprex v2.1.1
Suggested solution
In the default implementation, data gets passed down to
vec_math_base()
which calls the function from base, and then the output is restored to the input type. I cannot see an issue fromvec_math_base()
if we allow integer (or even complex) arguments through, but the Math operations on integers do not necessarily return integers (e.g.log(2L)
andmean(1:10)
both return something that cannot be restored to integer type).Would there be concerns with removing the
vec_restore()
fromvec_math.default()
and allowing all numeric types through?Finally, I've started using vctrs hands-on to build a class, and it's a great package and I appreciate all the work on it!