Non-Contradiction / JuliaCall

Embed Julia in R
https://non-contradiction.github.io/JuliaCall/index.html
Other
265 stars 36 forks source link

Rcpp-like interface #83

Open jeffreypullin opened 5 years ago

jeffreypullin commented 5 years ago

Hi Non-contradiction,

Thanks for the great package! I was just wondering if there were any thoughts about implementing a more Rcpp like interface. By that I mean having julia functions in a file with some sort of export tag and then a function to generate R wrappers around them. I don't think that would be super difficult to implement and would be an amazing interface to have.

Something like:

# JuliaCall::Export
function j_add(x, y)
   x + y
end

would generate:

j_add <- function(x,y) {
    julia_call(j_add, x, y)
}

which could then be used in R as normal

Thanks!

Non-Contradiction commented 5 years ago

A great idea! I think the function can be generated by extracting comments from the Julia file without much difficulty. The only thing I'm not sure about the signature of the function. I mean, it is easy to generate

j_add <- function(...) {
    julia_call("j_add", ...)
}

But not quite easy to generate

j_add <- function(x, y) {
    julia_call("j_add", x, y)
}

This is because since Julia uses multiple dispatch, a function can have multiple methods for different types and number of arguments and etc. So if we want export Julia functions which have multiple methods, what should the signature of the exported functions be? In fact, Rcpp faces the same problem, but it deals with it by restricting that the exported function can only have one method. But I'm not sure that we should do the same thing in Julia because multiple dispatching is so fundamental.

jeffreypullin commented 5 years ago

Great! Well I'd be happy to work on the Julia code parsing function (when I get the time).

Unfortunately, (due to my only limited knowledge of how Julia's multiple dispatch works) I don't quite follow your second point. Wouldn't the Julia code compile into a function with multiple methods which would be dispatched differently depending on what is passed through the R function?

i.e.

j_add(1, 2)

would dispatch differently than

j_add(as.integer(1), as.integer(2))

despite having the same arguments?

What am I missing?

All the best!

Non-Contradiction commented 5 years ago

Yes, the function will be dispatched in Julia according to the argument type. But what should the arguments of the exported R function be when the two Julia methods have different numbers of arguments or different names of arguments? For example, if j_add have two methods like

function j_add(x, y)
    x + y
end

function j_add(s1::AbstractString, s2, s3)
    s1 * s2 * s3
end

What should the exported R function be? j_add <- function(x, y){...} or j_add <- function(s1, s2, s3){...}?

jeffreypullin commented 5 years ago

Ah, okay - I didn't realise that different methods could take different arguments. I think the best workaround would be to generate the ... form by default and then have some documentation about how users could write argument handling code in R if needed. Does that sound alright?