mutable struct myTp
a::Vector{Int64}
b::Tuple{Int64}
#myTp() = new([], ())
myTp(a,b) = new(a,b)
end
function main()
c=myTp([1,2,3],(4,5,6))
end
main()
Can anyone let me know why?
mutable struct myTp
a::Vector{Int64}
b::Tuple{Int64}
#myTp() = new([], ())
myTp(a,b) = new(a,b)
end
function main()
c=myTp([1,2,3],(4,5,6))
end
main()
Can anyone let me know why?
Consider:
julia> typeof((1,2,3))
Tuple{Int64,Int64,Int64}
julia> typeof((1,))
Tuple{Int64}
julia> typeof((1,2,3))==typeof((1,))
false
This means that the type of the tuple is dependent on all its components. Note that this is different to Vector
. Maybe you’re looking for this:
julia> mutable struct MyTp{N}
a::Vector{Int64}
b::NTuple{N,Int64}
end
julia> MyTp([1,2,3],(4,5,6))
MyTp{3}([1, 2, 3], (4, 5, 6))
N
in mutable struct MyTp{N}
is the same as T
in
mutable struct Point{T<:Real}
x::T
y::T
end
They are called parametric constructors.
mutable struct myTp{N}
a::Vector{Int64}
b::NTuple{N,Int64}
#myTp() = new([], ())
#myTp(a,b) = new(a,b)
end
function main()
c=myTp([1,2,3],(4,5,6))
end
main()
Got an error " invalid redefinition of constant myTp".
Restart the REPL
This is something I do not quite understand. Why restarting the REPL would solve it?
Because you cannot redefine types:
julia> struct A
a
end
julia> struct A
a
b
end
ERROR: invalid redefinition of constant A
Stacktrace:
[1] top-level scope at none:0
If I cannot redefine types, that means that I must get the structure correct on the first time. When restarting the REPL, one loses a few minutes for various precompilations. That is very suboptimal in terms of workflow. What is a way around this. Thanks.
Two things: