JJTO
JJTO

Reputation: 897

MethodError: 'convert' has no method mathing convert(::T) when instantiating a type

I have a composite type like this:

type CPUEmulator
  memory::Vector{Int64}

  CPUEmulator(memory::Vector{Int64}) = new(memory)
end

When I try to instantiate it like this,

myCPU = CPUEmulator([1,2])

I get the following error:

LoadError: MethodError: `convert` has no method matching convert(::Type{CPUEmulator}, ::Array{Int64,1})
This may have arisen from a call to the constructor CPUEmulator(...),
since type constructors fall back to convert methods.
Closest candidates are:
  CPUEmulator(::Any, !Matched::Any)
  call{T}(::Type{T}, ::Any)
  convert{T}(::Type{T}, !Matched::T)

In the documentation example, you have a type:

type Foo
  bar
  end
end

And you can instantiate it like this:

foo = Foo(1,2)

I can't figure what I have to do differently to instantiate my custom type

Upvotes: 1

Views: 96

Answers (1)

Michael Ohlrogge
Michael Ohlrogge

Reputation: 10990

I cannot reproduce this error - the code that you have works fine on my computer. One thing about Julia is that it does not allow you to redefine custom types. It appears from the error message that the compiler is still working off an earlier definition of the CPUEmulator type that does not use the Vector{Int64} type specification that you have in the custom type definition and constructor you have in your question.

Thus, if you already entered one definition for type CPUEmulator and you are working in a given REPL session, for instance, you will need to close that REPL and reopen it. Alternatively, you can just run your Julia program from the start with, e.g. julia myscript.jl.

Upvotes: 3

Related Questions