Static methods

Imagine there are a bunch of static methods (a la C++/Java) defined like:

foo(::Any) = 0
foo(::A) = 1
foo(::T) where (T <: B} = 2
foo(::C) = 3

function trampoline(t) 
 ... large code chunk here that doesn't depend on t...
 foo(t)
 ... more code here that doesn't depend on t...
end

I think this is called the parametric type pattern in the book by Tom Kwong.
I would like to avoid specializing & recompiling trampoline(t) for every invoked parameter type. Is there any manner to avoid that, assuming that foo(x) is a static function (it doesn’t depend on any instance field).

See the @nospecialize macro.

(For the vast majority of functions, this is not beneficial. In normall well written Julia code, you shouldn’t find yourself recompiling functions over and over or doing lots of dynamic dispatch.)

4 Likes

I would use

function chunk1(#=args excluding t=#)
    # ... large code chunk here that doesn't depend on t...
end
function chunk2(#=args excluding t=#)
    # ... large code chunk here that doesn't depend on t...
end

function trampoline(t, args...)
    chunk1(args...)
    foo(t)
    chunk2(args...)
end
9 Likes