Avoid re-compiling functions with specialized types

suppose we have the following classes:

struct A; end
struct B; end
struct C{T}; x; end

function foo(c::C); c.x = 1; end

does invoking foo(C{A}()) and foo(C{B}()) still cause the compiler to generate two specialized functions, one for A and the other for B ? If so, is there any difference in writing foo(c::C) and foo(c::C{T)) where {T}, besides the obvious case when T is referred in the body of foo for whatever reason?

yes. There is a @nospecialize macro to avoid specialization, but in general I do not think one wants to avoid that. There should be a very special reason for not wanting that specialization. (see here)

no

2 Likes