Redefinition of functions sometimes possible?

When you write

f(x) = x + 1

you can think of this as ‘special syntax’ for something more like

struct var"typeof(f)" end
const f = var"typeof(f)"()
(::typeof(f))(x) = x + 1

Here’s that in action:

julia> struct var"typeof(f)" end

julia> const f = var"typeof(f)"()
var"typeof(f)"()

julia> (::typeof(f))(x) = x + 1

julia> f(1)
2

In julia, methods can be attached to any object. Functions are just a type of singleton object that have special syntax to make it easier to add methods to them.

4 Likes