Dear community,
I’m trying to define a type that behaves as a function. The usual way is something like
struct T
z::Int
end
(t::T)(x) = x * t.z
Then, if t = T(2)
we have t(3) == 6
.
However there is one way in which t
is not like a function. It is not a const
. Hence, if I do f(x) = t(x)
, then f
is not inferred (it relies on a non-const global)
julia> @code_warntype f(2)
Variables
#self#::Core.Compiler.Const(f, false)
x::Int64
Body::Any
1 ─ %1 = Main.t(x)::Any
└── return %1
In contrast, t(2)
infers fine. If t
were a regular function, not just a callable type, f
would be inferred, because functions are not only callable, they are const
s.
The question: is there any way to define T
so that t = T(2)
behaves as const t = T(2)
(like when one defines a regular function), without requiring that the user specify that const
?