I wanted to test how abstract type works in "strcutres", so I created a struct below with a constructor, however, the return message displays "ERROR: MethodError: no method matching Rect(::Int64)".
I don't know which part went wrong, although the parameter "5" that I used fits the definition of the constuctor which is a single integer input. Thank you.
abstract type Shape{T<:Integer,F<:AbstractFloat} end
export Shape
struct Rect{T,F} <: Shape{T,F}
a::T
b::T
c::T
v::F
function Rect{T,F}(a::T) where {T<:Integer, F<:AbstractFloat}
b = 10;
c = 10;
v = a*b*c;
return new(a,b,c,v)
end
end
function main()
vol = Rect(5).v;
println(vol)
end
main()
It should return a product of a, b and c with only a being the input variable.
You do not have the appropriate constructor. You should do:
julia> Rect{Int,Float64}(5)
Rect{Int64, Float64}(5, 10, 10, 500.0)
Other option is to add the constructor to your struct:
function Rect{T}(a::T) where {T<:Integer}
Rect{Int,Float64}(a)
end
Once you add the constructor you can do:
julia> Rect{Int}(5)
Rect{Int64, Float64}(5, 10, 10, 500.0)
Or you can also define:
Rect(a::Int) = Rect{Int,Float64}(a)
and have
julia> Rect(5)
Rect{Int64, Float64}(5, 10, 10, 500.0)