Skip to content

Enhance Rcpp::Nullable with explicitly cast as() export #1470

@eddelbuettel

Description

@eddelbuettel

In #1451 @cgiachalis suggested an Armadillo variant using a lambda which got us riffing a little. As shown in the (final ?) example, if we just enhance the existing Nullable::as() a little we can make this really right:

// [[Rcpp::export]]
arma::mat bis(Rcpp::Nullable<arma::mat> m = R_NilValue) {
    return (m.isNotNull() ? m.as() : arma::mat{0,0, arma::fill::none});
}

This utilizes a 'thirteen char' addition we should add.

modified   inst/include/Rcpp/Nullable.h
@@ -126,7 +126,7 @@ namespace Rcpp {
         /**
          * Returns m_sexp as a T
          */
-        inline T as() { return get(); }
+        inline T as() { return Rcpp::as<T>(get()); }
 
         /**
          * Return a clone of m_sexp as a T

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions