Need help understanding creation and usage of Structs

I think you wanted htype::String, lower-case string is a function not a type.

Also, heaptype != "min" || heaptype != "max" is precisely false, maybe you wanted &&.

Then it runs:

julia> mutable struct BinaryHeap{T<:Real}
           htype::String
           heap::Vector{T}
           BinaryHeap{T}(heaptype="min") where T = (heaptype != "min" && heaptype != "max") ? error("Invalid heap type") : new(heaptype, Vector{T}(undef, 0))
       end

julia> BinaryHeap{Int}()  # now runs
BinaryHeap{Int64}("min", Int64[])

julia> methods(BinaryHeap{Int})  # default has already made a method with 0 args
# 2 methods for type constructor:
[1] BinaryHeap{T}() where T in Main at REPL[8]:4
[2] BinaryHeap{T}(heaptype) where T in Main at REPL[8]:4

julia> methods(BinaryHeap)
# 0 methods for type constructor:

But the point of inner constructors is to make sure there is no other way to make the struct, except through them. Your last method tries to do that, and it fails. It’s trying to call what would be one of the default constructors had you not provided an inner constructor. It could instead construct & then mutate, but probably there are better ways.

julia> BinaryHeap{T}(v::Vector{T}) where T = BinaryHeap{T}("min", v)

julia> BinaryHeap{Int}([1,2,3])
ERROR: MethodError: no method matching BinaryHeap{Int64}(::String, ::Vector{Int64})
4 Likes