"Type function"/"functor" -- where is info in documentation?

I’m trying to find the correct name/documentation for defining “type functions” – any hint on where I can find it?

As an example, consider the following function for computing some norms of a vector:

function (v::Vector)(s::Symbol)
    if s == :one
        return sum(abs.(v))
    elseif s == :two
        return sqrt.(sum(v.^2))
    elseif s == :inf
        return maximum(abs.(v))
    else
        println("not defined")
    end
end

Then,

julia> v = [1,2,3]
julia> v(:one), v(:two), v(:inf)
(6, 3.7416573867739413, 3)

It is probably not advisable to create such functions for standard Julia types, but my question is rather:

  • What is such a function construct called in the Julia documentation? In other words, where is it documented?

https://docs.julialang.org/en/v1/manual/methods/#Function-like-objects

3 Likes

Should be safe if you don’t do type piracy, but people prefer the new types to have callable instances instead of the preexisting types that didn’t have callable instances.

1 Like

Thanks. I didn’t search for “object” since Julia is not “object oriented” in the standard way…

I’m no specialist on this, but I think an “object” is an instance/realization of a “class”.

So in Julia, “types” are somewhat close to a “class” (except for the methods vs. dispatch). Is there a generic Julia word for “instantiated”/“constructed” type? I.e., Vector is a type (“class”). Would v = [1,2,3] be referred to as an “object”? Or something else?

This is the typical meaning going way back to Simula 67’s introduction of OOP. However, “object” has since been used outside of OOP e.g. C. The Common Lisp Object System (CLOS) is considered OOP, but methods belong to multiply-dispatched generic functions instead of classes. That is indeed a major inspiration for Julia, but a key difference is that CLOS has class inheritance, but Julia’s concrete types cannot subtype another concrete type. That’s why Julia’s types are not classes; Go seems to draw that same line. Despite not having classes, Go and Javascript have method encapsulation and are still considered OOP. Lines are hard to draw and it’s hard to tell where the tipping point is, but Julia is far enough that nobody considers it OOP.

The computer science term for the result of instantiation is instance. The Julia documentation uses both terms interchangeably; the shortest example of this I could find:

Constructors that don’t return instances of their own type
In very rare cases it might make sense for the constructor T(x) to return an object not of type T

1 Like