Constructors - cannot convert from type to object


#1

I’m trying to create a tree class using Dicts. The base struct is:

mutable struct MyNode
    name::String
    information::Tuple
    children::Dict{String, MyNode}
end

I have a constructor:

MyNode(name::String) = MyNode(name, (), Dict{String, MyNode}())

If I try to construct a MyNode from the REPL like so:

> tree1 = MyNode("root", (), Dict{String, MyNode}())

it works. But if I try to use the constructor I get an error:

> tree = MyNode("root")
ERROR: MethodError: Cannot `convert` an object of type Type{Dict{String,MyNode}} to an object of type Dict{String,MyNode}
This may have arisen from a call to the constructor Dict{String,MyNode}(...),
since type constructors fall back to convert methods.

What am I doing wrong?


#2

Your code works perfectly here (BTW, thanks for the reproducible test case!). Looking at the error message, I suspect you forgot () after Dict{String, MyNode} at some point during your testing.


#4

Errr. So quitting the REPL and restarting, and you are correct. It works. I did, in my first iteration, forget the trailing parens. But why didn’t it get loaded over when I reloaded the module file?