Constructors - cannot convert from type to object


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

mutable struct MyNode
    children::Dict{String, MyNode}

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?


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.


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?