-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Implements Rfloat
and Doubles
using macros
#284
Conversation
This is great! One comment about doc strings: I understand the reasoning that they shouldn't be used to test the code, and I agree. However, I think 1-2 examples per function are super helpful and make the code more accessible for a new user. So I wouldn't just delete all these examples. Rather assess whether the example is actually helpful or not. |
@clauswilke, I agree about examples. The challenge is -- when members are generated using macros, we have two options -- generate examples the same way we generate all these methods right now, which can be tricky, or add additional parameters to the macros, which are translated into docs. |
I see. Maybe it's better to write documentation by hand and to just put all the relevant info and examples into a single documentation block for the entire type. In other words, put the documentation and examples for extendr/extendr-api/src/wrapper/doubles.rs Lines 4 to 7 in 6549281
|
@clauswilke, |
Looking good! I am usually cautious about using extra dependencies, but the problems with It is worth thinking about Logicals/Rbool (aka Bool) but not in this PR. This shares much with Integers. We may need to implement more operators in the future (such as &, |, ^) for integer and bool. |
I have identified several issues with |
Downside - can no longer compare I also generated some doctests using macros, to test simple behavior (like Next stop: bounds check when using |
I don't see this as a downside. As you say, it's exactly how R handles the same case. |
I agree that Rint::na() should not (partial) compare with another Rint::na() This behavious makes it easy to compare floats. |
a5b3e24
to
64dff42
Compare
fn double_scalar(x: f64) -> f64 { | ||
x | ||
} | ||
|
||
// Convert an int scalar to itself | ||
// x a number | ||
#[extendr] | ||
fn int_scalar(x: i32) -> i32 { x } | ||
fn int_scalar(x: i32) -> i32 { | ||
x | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These formatting changes were unintentional, I hooked cargo fmt
to OnSave of my CLion, so once I edited this file, it automatically applied formatting and I did not notice it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor comments.
Co-authored-by: Hiroaki Yutani <yutani.ini@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel this exceeds my level of technical understanding so please take my approval as nothing more than saying I'm very much in favor of this work and am glad you're taking the lead.
One minor comment: I always find it hard to read macro code and I would encourage you to be extra careful with documenting. It may seem all obvious to you today as you're writing this but a few more comments probably wouldn't hurt. At a minimum, I think it would be good for every chunk of macro code (i.e., every impl
section) to provide in a comment a concrete example of what it implements. For example: "Implement binary mathematical operators such as + or -: a + b"
* rebase on master * I hope this explains the drop test. * More comments for Claus. * Fix typos * fmt * More comments. * Rashly edit the merge conflict in github. * fmt * [WIP] Implements `Rfloat` and `Doubles` using macros (#284) * Generalizing macros * Generating 'From' * Fixed typo * More macros * Preliminary implementation of Rfloat * First Rfloat tests * Refactored macros * Split 'impl' macros into two * Instance methods for Rfloat * Mixed bianry operators * Testing Rfloat * Formatting * Gnenerating altrep implementations * Adding support for Doubles * Comments * Using 'paste!' to generate vectors * Implementing 'Doubles' using macros * Ported 'Integers' to new macros * Generating default value * Formatting * Adding comments to the macros * Simplified unary operator macros * Simplified binary operator macros * [POC] generating doc tests on the fly * Implementing 'Default' for scalars * Some docs * Changed how 'NA's are handled * Updated scalars tests * Defensive check on 'vector::elt' + doctests for 'elt' * Testing 'Doubles' * Testing out of range * Testing Integers/Doubles from R * Updated tests * Fixed docs Co-authored-by: Hiroaki Yutani <yutani.ini@gmail.com> * Note on 'elt' boundary checks [skip ci] Co-authored-by: Hiroaki Yutani <yutani.ini@gmail.com> * rebase on master * I hope this explains the drop test. * More comments for Claus. * Fix typos * fmt * More comments. * Rashly edit the merge conflict in github. * fmt * Fix merge conflict. * Merge Co-authored-by: Ilia <ilia.kosenkov@outlook.com> Co-authored-by: Hiroaki Yutani <yutani.ini@gmail.com>
Fixes #280.
This turned out to be a much larger contribution than I expected.
After implementing
Doubles
, I decided to introducepaste
as a dependency (see #283) and then further improved code generation.Major topics:
Rfloat
as a thinNA
-aware wrapper aroundf64
. I generalized macros so that bothRint
andRfloat
are generated using the same macros, and the only thin written by hand isTryFrom
that also allows coercion (i.e., floats that can be represented as ints can be converted toRint
).Doubles
.Doubles
is an almost exact copy ofIntegers
, so, with the help ofpaste
, bothDoubles
andIntegers
are generated using a single macros. I expect this approach will work as well for other primitive types. If not, at least in these cases we do not write the same code twice.test
module. When members are generated using macros, it is even harder to generate meaningful tests. In this case, I took out test cases, and put them in a separate module (undercrate::wrappers::{integers, doubles}::tests
). These tests are guarded with#[cfg(test)]
and are now executed in all build scenarios (seeS4
doctests potentially fail on Windows #282).Takeways:
impl
membersTODO
)TODO:
extendr-api/tests
tests/extendrtests