Composite types and their subtypes

I’m still getting used to the type system in Julia. Is the following composite type,

mutable struct Point{Float64}
    x
    y
end

equivalent to

mutable struct Point
    x::Float64
    y::Float64
end

It seems like the answer is no?

You’re correct that the answer is no. See: https://docs.julialang.org/en/v1/manual/types/#Parametric-Types-1

Your first type has a type parameter with the name Float64 which does absolutely nothing. The fields x and y can still be of any type. Moreover, the fact that the type parameter has a name which is the same as an existing type (Float64) is totally irrelevant.

Your second type, on the other hand, actually restricts the x and y fields to be of type Float64.

To implement a parametric Point type, you want something like this:

mutable struct Point{T}
  x::T 
  y::T 
end

which creates a struct with a parameter named T and two fields which must both be of that same type T.

2 Likes