diff --git a/ChangeLog b/ChangeLog index 792188076..52971a50a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,13 @@ 2026-04-09 Dirk Eddelbuettel * DESCRIPTION (Version, Date): Roll micro version and date + * inst/include/Rcpp/Nullable.h (T): Also add 'operator T()' + (conditional on R (>= 4.3.0) as older versions croak) + +2026-04-08 Dirk Eddelbuettel + + * inst/include/Rcpp/Nullable.h (as): Enhance by adding an explicit + Rcpp::as() aiding cases where implicit as<> does not suffice 2026-04-07 Dirk Eddelbuettel diff --git a/inst/include/Rcpp/Nullable.h b/inst/include/Rcpp/Nullable.h index 53704ffb8..27085b8fa 100644 --- a/inst/include/Rcpp/Nullable.h +++ b/inst/include/Rcpp/Nullable.h @@ -81,6 +81,18 @@ namespace Rcpp { return m_sexp; } +#if R_VERSION > R_Version(4,3,0) + /** + * operator T() to return nullable object + * + * @throw 'not initialized' if object has not been set + */ + inline operator T() const { + checkIfSet(); + return Rcpp::as(m_sexp); + } +#endif + /** * get() accessor for object * @@ -126,7 +138,7 @@ namespace Rcpp { /** * Returns m_sexp as a T */ - inline T as() { return get(); } + inline T as() { return Rcpp::as(get()); } /** * Return a clone of m_sexp as a T diff --git a/inst/tinytest/test_misc.R b/inst/tinytest/test_misc.R index 9fb5f50d9..08df4b5cd 100644 --- a/inst/tinytest/test_misc.R +++ b/inst/tinytest/test_misc.R @@ -168,7 +168,7 @@ expect_equal( testNullableIsUsable(M), M) # test.NullableIsUsableFalse <- function() { expect_true(is.null(testNullableIsUsable(NULL))) -# test.NullableString <- function() { +## test.NullableString <- function() { expect_equal(testNullableString(), "") expect_equal(testNullableString("blah"), "blah")