Le mardi 07 juillet 2015 à 02:29 -0700, Simon Byrne a écrit :
> Thanks everyone.
> 
> A bit more context: I'm trying to implement callbacks in RCall.jl. 
> There are multiple ways R objects could be converted to Julia ones 
> (as singletons, vectors, Nullable singletons/vectors, dictionaries, 
> etc.), so what I was thinking was:
> 1) pass the R objects (known as SEXPRECs) straight to the Julia 
> method
> 2) the first time a Julia function is converted to an R object (via 
> the sexp function), I wanted to define an initial generic method 
> which would take SEXPRECs and perform a default conversion. 
> 
> function sexp(f::Function)
>     if method_defined(f,(VarArgs{SEXPREC},))
>         global f
>         f(x::SEXPREC...)
>             y = rcopy(...) # do default conversions
>             f(y...)
>         end
>     end
>     return callback(f) # construct R object for callback
> end
> 
> In that way if a user wanted a different conversion, they could 
> define their own method(s) to do this. But I don't want to overwrite 
> this if it already exists.
> 
> Any ideas?
Interesting design issue!

I think another solution could work, but I'm not sure it's really
better. Instead of checking whether
method_defined(f,(Vararg{SEXPREC},))
you could always define a fallback wrapper function like this:
function f(x::AbstractSEXPREC...)
     y = rcopy(x...) # do default conversions
     f(y...)
end

AbstractSEXPREC would just be an abstract type from which SEXPREC would
inherit, so that the fallback would not be called when a more specific
f(x::SEXPREC...) method exists.

Then, you would call f() on the VarArgs{SEXPREC}, and Julia would take
care of calling either the user-defined function or your fallback,
based on the standard dispatching rules.

As I said, your solution might be equally good.


My two cents


> -Simon
> 
> 
> On Monday, 6 July 2015 19:09:43 UTC+1, Mauro wrote:
> > If I understand correctly what you want, then have a look at 
> > Traits.jl 
> > where I check method signatures of trait-definitions against 
> > methods of 
> > a generic function.  See the loop at: 
> > https://github.com/mauro3/Traits.jl/blob/master/src/Traits.jl#L158 
> > and 
> > in particular isfitting. 
> > 
> > Turns out that this is a relatively hard problem (unless I made a 
> > mess 
> > of it), in particular once parametric methods are involved. 
> >  However, 
> > your problem might be a bit easier than what Traits does as it test 
> > for 
> > equality.  Let me know if you got questions. 
> > 
> > On Mon, 2015-07-06 at 19:25, Simon Byrne <[email protected]> 
> > wrote: 
> > > If I have a generic method foo, is there a way I can tell if a 
> > particular 
> > > signature has been defined? 
> > > 
> > > Note that I don't want method_exists (which simply determines if 
> > something 
> > > can be dispatched), I want to determine if a particular 
> > definition has been 
> > > made, e.g. if 
> > > 
> > > foo(x) = x 
> > > 
> > > then I want 
> > > 
> > > method_defined(foo,(Int,)) == false 
> > > method_defined(foo,(Any,)) == true 
> > 

Reply via email to