Instantiation of a subtype of an abstract type

I’m following the MIT class “Introduction to Computational Thinking”, and have some trouble with the recent homework. (The class is amazing by the way. Any applied math course should be taught in this way IMO.)

A structure is defined as follows.

struct RankOneMatrix{T} <: AbstractMatrix{T}

If we immediately construct an object using this type, we will get errors.
We need to extend Base.size() and Base.getindex() first. Then the object of this type can be constructed.
Is this the canonical way to inherit an abstract type like AbstractMatrix?
Or there is a better way to do the same thing?

The reason you are getting an error is that when you create an object in the repl, Julia prints it out to show what you did. In order to print an AbstractMatrix, Julia needs to know how big it is, and how to get the elements.

1 Like

Thanks, it makes sense now.

I just tested this in a script.
No error shows up until I want to print the whole matrix.

1 Like