Functions with additional parentheses

I noticed this syntax in the DynamicHMCExamples package.

function (problem::LogisticRegression)(θ)
    @unpack y, X, σ = problem
    @unpack β = θ
    loglik = sum(logpdf.(Bernoulli.(logistic.(X*β)), y))
    logpri = sum(logpdf.(Ref(Normal(0, σ)), β))
    loglik + logpri
end

Please can someone explain what this does - I haven’t found any instances of this form of declaration elsewhere.

1 Like

It is this: https://docs.julialang.org/en/v1/manual/methods/index.html#Function-like-objects-1

Methods are associated with types, so it is possible to make any arbitrary Julia object “callable” by adding methods to its type. (Such “callable” objects are sometimes called “functors.”)

5 Likes

I should probably explain things like this in the package docs, which will eventually evolve into an introduction for programming, debugging, and optimizing your own log posteriors in Julia.

Thank you both!