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

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

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

1 Like

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

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:

  1. You must have been warned by Discourse to not reply to old discussions to avoid bothering all the old participants of the discussion. This is to be avoided. Create new topics instead.
  2. A quick search in google or in discourse return this answer.
1 Like
  1. Thanks. I was not warned.
  2. Thanks. I had read and did not understand the answer. I now put my structures in a separate module.