Can not convert an object of type Tuple{Int64, Int64} to an object of type Tuple{}

#1
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?

0 Likes

#2

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))
2 Likes

#3

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.

0 Likes

#4
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".

0 Likes

#5

Restart the REPL

1 Like

#6

This is something I do not quite understand. Why restarting the REPL would solve it?

0 Likes

#7

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
5 Likes