Skip to content

Feature converter ints doubles #201

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

Merged
merged 13 commits into from
Jul 9, 2021

Conversation

sbearrows
Copy link
Contributor

Might better to name them as_doubles() and as_integers() to differentiate from scalar double and int?? When I first was making it that sounded odd to me but now I'm not so convinced.

fixes #46

@jimhester
Copy link
Member

I think you are right, as_doubles() and as_integers() seems like a good idea to me.

@jimhester
Copy link
Member

Looks like you have a blank newline that is causing the format_check job to fail https://github.com/r-lib/cpp11/pull/201/checks?check_run_id=3024080555#step:5:11.

FWIW in RStudio you can enable showing whitespace characters with

Tools -> Global Options -> Code -> Display, check "Show whitespace characters".

Which could help spot these more easily.

@sbearrows
Copy link
Contributor Author

Okay I'm going to make those changes to function names. Any insights you might have on the ubuntu 3.4 error? I can't really tell where or why it's occurring.

@jimhester
Copy link
Member

I was able to reproduce it locally with R 3.4, it seems like an issue related to cpp11::stop(). That function requires unwind_protect, which IIRC has some issues on R versions before 3.5.

Switching the errors to throw regular C++ exceptions instead fixes the test failures.

I think it might be worth changing all the uses of cpp11::stop() in the cpp11 code to throw exceptions instead. In general I think cpp11:stop() seems to work fine for user code, but perhaps it would be better for the internal cpp11 code not to rely on it.

@@ -148,8 +148,7 @@ inline doubles as_double(sexp x) {
return ret;
}

// else
stop("Expected a numeric vector for");
throw type_error(INTSXP, TYPEOF(x));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cpp11 defines a type_error class to generate a more informative error message for type issues, so we can use that here.

class type_error : public std::exception {
public:
type_error(int expected, int actual) : expected_(expected), actual_(actual) {}
virtual const char* what() const noexcept {
snprintf(str_, 64, "Invalid input type, expected '%s' actual '%s'",
Rf_type2char(expected_), Rf_type2char(actual_));
return str_;
}
private:
int expected_;
int actual_;
mutable char str_[64];
};

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay awesome thank you!

@sbearrows
Copy link
Contributor Author

Okay I will make a new issue to change instances of cpp11::stop to C++ style exceptions.

Copy link
Member

@jimhester jimhester left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, feel free to merge it once you are ready.

@sbearrows sbearrows merged commit 5fb3137 into r-lib:master Jul 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Permissive handling of integers in cpp11::integers
2 participants