<p dir="auto">I cooked up a minimal example (a modified example from the julia docs) in order to show my problem:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="func_dict = Dict{Int,Function}()

function mycustomsort{T}(a::T, b::T)
  return func_dict[1](a,b)
end

# Last chance to define mycompare here(!!)

const mycustomsort_c = cfunction(mycustomsort, Cint, (Ref{Cdouble}, Ref{Cdouble}) )

# Too late; the world has changed!
function mycompare{T}(a::T, b::T)
  return convert(Cint, a&lt;b ? -1 : a&gt;b ? +1 : 0)::Cint
end

func_dict[1] = mycompare

A = [1.0,5,2,3]

ccall(:qsort, Void, (Ptr{Cdouble}, Csize_t, Csize_t, Ptr{Void}), 
        A, length(A), sizeof(eltype(A)), mycustomsort_c)
println(A)"><pre class="notranslate"><code class="notranslate">func_dict = Dict{Int,Function}()

function mycustomsort{T}(a::T, b::T)
  return func_dict[1](a,b)
end

# Last chance to define mycompare here(!!)

const mycustomsort_c = cfunction(mycustomsort, Cint, (Ref{Cdouble}, Ref{Cdouble}) )

# Too late; the world has changed!
function mycompare{T}(a::T, b::T)
  return convert(Cint, a&lt;b ? -1 : a&gt;b ? +1 : 0)::Cint
end

func_dict[1] = mycompare

A = [1.0,5,2,3]

ccall(:qsort, Void, (Ptr{Cdouble}, Csize_t, Csize_t, Ptr{Void}), 
        A, length(A), sizeof(eltype(A)), mycustomsort_c)
println(A)
</code></pre></div>
<p dir="auto">One gets the output:</p>
<blockquote>
<p dir="auto">ERROR: LoadError: MethodError: no method matching mycompare(::Float64, ::Float64)<br>
The applicable method may be too new: running in world age 20393, while current world is 20394.</p>
</blockquote>
<p dir="auto">Yes in the (constructed) example above, one can use the workaround to define <code class="notranslate">mycompare</code> before <code class="notranslate">mycustomsort_c</code>. But in real world examples this is not always possible.</p>
<p dir="auto"><em>Real world example:</em> I'm the author of the ODEInterface package (available for Julia v0.4, v0.5, and trunk before 23.12.2016). There the idea is, that the user can write julia functions (e.g. the right-hand side of an ODE) and give this julia-functions to the ODEInterface. Inside the ODEInterface Fortran codes are called. These Fortran-Codes call Callback-functions that (in my case) are cfunctions of the ODEInterface that in turn try to call the user-given Julia function. Since Pull <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="161551964" data-permission-text="Title is private" data-url="https://github.com/JuliaLang/julia/issues/17057" data-hovercard-type="pull_request" data-hovercard-url="/JuliaLang/julia/pull/17057/hovercard" href="https://github.com/JuliaLang/julia/pull/17057">#17057</a> this is not possible anymore, because the ODEInterface (and hence the cfunctions) are typically loaded/compiled before the user defines the problems he/she wants to solve.</p>
<p dir="auto">Any idea how I can solve this problem?</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Julia Version 0.6.0-dev.1704
Commit 0a0c41c* (2016-12-27 16:34 UTC)
Platform Info:
  OS: Linux (x86_64-redhat-linux)
  CPU: Intel(R) Core(TM) i7-4700MQ CPU @ 2.40GHz
  WORD_SIZE: 64
  BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell)
  LAPACK: libopenblas64_
  LIBM: libopenlibm
  LLVM: libLLVM-3.7.1 (ORCJIT, haswell)"><pre class="notranslate"><code class="notranslate">Julia Version 0.6.0-dev.1704
Commit 0a0c41c* (2016-12-27 16:34 UTC)
Platform Info:
  OS: Linux (x86_64-redhat-linux)
  CPU: Intel(R) Core(TM) i7-4700MQ CPU @ 2.40GHz
  WORD_SIZE: 64
  BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell)
  LAPACK: libopenblas64_
  LIBM: libopenlibm
  LLVM: libLLVM-3.7.1 (ORCJIT, haswell)
</code></pre></div>
<p dir="auto">It seems much harder to pass a proper closure as a callback function in Julia 0.6 than in previous versions due to <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="161551964" data-permission-text="Title is private" data-url="https://github.com/JuliaLang/julia/issues/17057" data-hovercard-type="pull_request" data-hovercard-url="/JuliaLang/julia/pull/17057/hovercard" href="https://github.com/JuliaLang/julia/pull/17057">#17057</a>.  In particular, the method <del>described in the manual and</del> <a href="http://julialang.org/blog/2013/05/callback" rel="nofollow">in this blog post</a> no longer works: passing the <code class="notranslate">Function</code> via a "thunk" <code class="notranslate">void*</code> that is called from a <code class="notranslate">cfunction</code> fails because the <code class="notranslate">Function</code> sees an "old world age".</p>
<p dir="auto">See <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="198098805" data-permission-text="Title is private" data-url="https://github.com/JuliaPy/PyCall.jl/issues/343" data-hovercard-type="issue" data-hovercard-url="/JuliaPy/PyCall.jl/issues/343/hovercard" href="https://github.com/JuliaPy/PyCall.jl/issues/343">JuliaPy/PyCall.jl#343</a> and <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="198135795" data-permission-text="Title is private" data-url="https://github.com/JuliaMath/Cubature.jl/issues/22" data-hovercard-type="issue" data-hovercard-url="/JuliaMath/Cubature.jl/issues/22/hovercard" href="https://github.com/JuliaMath/Cubature.jl/issues/22">JuliaMath/Cubature.jl#22</a> and <a href="https://discourse.julialang.org/t/help-with-solving-a-world-age-error-in-package-on-julia-master/1126" rel="nofollow">the mailing list</a>, for example, but this will affect any package wrapping a C library that expects callback functions.</p>
<p dir="auto">In PyCall, <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/yuyichao/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/yuyichao">@yuyichao</a> suggested using <code class="notranslate">eval</code>, but this is clunky and seems likely to impose a large performance penalty.  Is there a better solution?</p>