Error during initialization



I hate to ask these kind of questions, but I can’t understand why this code doesn’t work. Apparently the problem is a stackoverflow error, which seems to be generated in the build_bar() function, when assigning. The new BarType is initially a vector of undef values, then I initialize it.
As far as I know, this should be legal, since I also tested a more complex code (with parametric types) which does likewise but works fine.
Any tip? Thanks!

using OffsetArrays

struct FooType <: AbstractArray{Float64,1}

FooType(n::Int) = FooType(OffsetArray(Float64, 1:n), n)
Base.size(s::FooType) = (s.dim,)
Base.IndexStyle(::Type{<:FooType}) = IndexLinear()
Base.similar(s::FooType, ::Type, d::Dims) = FooType(d[1])
Base.getindex(s::FooType, i::Int) =[i]
Base.setindex!(s::FooType, v, i::Int) = ([i] = v), s::FooType) = print(io, s)
Base.indices(s::FooType) = indices(
Base.:(==)(a::FooType, b::FooType) = ==
Base.isless(a::FooType, b::FooType) = a[1]<b[1]

struct BarType <: AbstractArray{FooType,1}

function build_bar(m::Int, n::Int)
bar = BarType(Array{FooType,1}(m), m)
for i in 1:m[i] = FooType(n)

BarType(m::Int, n::Int) = build_bar(m,n)
Base.size(b::BarType) = (b.dim,)
Base.IndexStyle(::Type{<:BarType}) = IndexLinear()
Base.similar(b::BarType, ::Type, d::Dims) = BarType(d[1])
Base.getindex(b::BarType, i::Int) =[i]
Base.setindex!(b::BarType, val, i::Int) = ([i] = val), b::BarType) = print(io, b.dim)
Base.indices(b::BarType) = indices(

b = BarType(3,2)


Your show method calls print which calls show which calls print which calls show etc, which is how you get a stack overflow error. This only happens at the end of your code when Julia tries to display your BarType and its contained FooTypes. The easiest thing to do is to just delete that show method since it’s doing nothing useful for you.

If you want a custom show() make sure that what you print() isn’t just the same thing that was passed to show(). For example, this works fine:

julia>, s::FooType) = print(io, "FooType with data: $(")

julia> b = BarType(3,2)
3-element BarType:
 FooType with data: [NaN, 0.0]
 FooType with data: [2.42531e-314, 2.29997e-314]
 FooType with data: [2.97131e-320, 0.0]