RcppCore / Rcpp

Seamless R and C++ Integration
https://www.rcpp.org
GNU General Public License v2.0
730 stars 210 forks source link

Leading underscores not allowed in parameter names #921

Closed gitclem closed 5 years ago

gitclem commented 5 years ago

I doubt anybody really will want to fix it and it's not really worth fixing, but the error message is that a naive programmer might doubt his sanity. The solution is simple: don't use a leading underscore.

#include <Rcpp.h>
// [[Rcpp::plugins("cpp14")]]

// [[Rcpp::export]]
Rcpp::NumericVector body_to_linear_cpp(Rcpp::NumericVector& _x
                   ){
  return _x;
}
Rcpp::sourceCpp("die.cpp")
Error in source(scriptPath, local = env) : 
  /tmp/RtmpXonAcW/sourceCpp-x86_64-pc-linux-gnu-1.0.0/sourcecpp_57dc704065ee/die.cpp.R:3:57: unexpected input
2: 
3: body_to_linear_cpp <- Rcpp:::sourceCppFunction(function(_
                                                           ^
eddelbuettel commented 5 years ago

That's clearer. I will remove the few posts that accidentally ended at the bottom of #138.

The issue is that identifiers in these interfaces have to be legal in both R and Cpp. If you use the _x style elsewhere it works:

R> Rcpp::cppFunction("double doubleMe(double x) { double _y = x + x; return _y; }")
R> doubleMe(21)
[1] 42
R> 

But because you cannot use a [leading] underscore on R identifiers :

R> _x <- 4
Error: unexpected input in "_"
R> 

you cannot use it in places that will also appear in R. Makes sense?

We could add an entry to the Rcpp FAQ but it is ex ante indeed not fixable.

(Edited to stress leading underscores which what I was talking about as it is the issue at hand.)

gitclem commented 5 years ago

Actually, the problem is you can't use leading underscores. R relaxed the use of underscores so that a_b is now legal. (I hadn't realized leading underscores are still illegal.)

> a_b <- 1
> a_b
[1] 1
eddelbuettel commented 5 years ago

You never could, and snake_case with middle _ is not exactly novel either.

The point is that what you tried never could work reasons that are not obvious at first, but entire rational.

So ok to close this?

kevinushey commented 5 years ago

The issue could probably be fixed by quoting the non-syntactic R identifiers. PRs welcome.

gitclem commented 5 years ago

Fine to close it, but please leave the thread. I'm sure another naive programmer will encounter this error and will be stumped.

eddelbuettel commented 5 years ago

Good point. I keep forgetting about the escaping backtick:

R> `_x` <- 4
R> `_x`
[1] 4
R> 

Second the notion that we're open to reviewing PRs but unlikely to write one here.

eddelbuettel commented 5 years ago

(Yes, I am strongly opposed to deleting issues and regret they added it. I am, as you can now tell, also opposed to randomly spamming 4 1/2 year old existing tickets. This one will remain, but the sad truth is that not too many people dig through older issues. Oh well.)

gitclem commented 5 years ago

I was searching for an explanation for the obscure error I found. I was going to make a new ticket, but I did see that it had been explained but had in the course of 4 years, had morphed. I was just trying to make it easier for the next befuddled programmer to find his way out... (I spent the better part of an afternoon unraveling this...)

What I have learned today, is Rcpp is a great tool, but because it's straddling the R and C++ world, there are gotchas. Thanks for your help.

eddelbuettel commented 5 years ago

Yes, sorry. You are not unreasonable in trying to expand the old ticket, but it may be cleaner to just start fresh.

As for the gotchas: Yup. I should earmark this for the Rcpp FAQ. It kinda belongs there and I don't think it is.

Thanks for the report. It is good to have ticket just in case, and better still with a very descriptive subject as you had chosen.