2
votes

I just started looking a Julia. I'm interested in how it accomplishes multiple dispatch, specifically how it determines the type for use in multiple dispatch.

In the introduction to Types,the manual states:

all values in Julia are true objects having a type that belongs to a single, fully connected type graph, all nodes of which are equally first-class as types

My question is: How is the type graph connected?

For example, where would an Array of Integers fit in? Is it a subtype of Array or a subtype of Integer? Both?

How 'bout Complex numbers? Can I have Arrays (2x1) of Rational and Duples (2-Tuples) of Integers both be Complex?

# This is my first Julia code ever. It is probably wrong.
abstract type Rational <: Number end
abstract type Integer <: Rational end
abstract type Complex <: Array{Integer, 2} end
abstract type Complex <: Tuple{Rational, Rational} end
(1, 2)::Complex * (1, 2)::Complex

In the above two examples, I'm using "code font" to denote abstract types.

Maybe the code example is the "wrong" concept? Maybe I should define a "multiply complex" function in stead?

Can you show me a drawing of the type graph for the built-in types?

1
Thank you, @phipsgabler. For other readers of this article, the abstract of the complicated article states "This paper provides the first formal definition of Julia’s subtype relation and motivates its design."Ana Nimbus

1 Answers

1
votes

Here is the number type tree taken from https://github.com/bkamins/The-Julia-Express This link also makes a good first reading.

enter image description here

In the type hierarchy you can see:

  • abstract types - these are to define the overall type structure. In Julia you can inherit only from an abstract type. Hence all branching types in this tree are abstract
  • concrete types - these are all the leaves
  • parametric types. These are the values that come after {}. T <: Integer means that any parametric type being a subtype of Integer will be accepted as a parametric type. Parametric types allow you to have things like Complex{Float64} and Complex{Int} - that is complex numbers defined over various type spaces. With Julia's multiple dispatch magic things like this "just work".

Hope this helps for the beginning. Another useful free resource is https://juliaacademy.com/