How to resolve `syntax: too few type parameters specified in "new{...}"`?

I am trying to define a tree node as a mutable struct in Julia v1.0, But I get the error

syntax: too few type parameters specified in "new{...}"

below is the MWE. Google points me to this https://github.com/JuliaLang/julia/issues/11310, but the content is quite cryptic, so how do I fix this error? I have a feeling that it has something to with T <: Real but I don’t how to fix.

module JLBoostTrees
    mutable struct JLBoostTreeNode{T <: Real}
        weight::T
        children::Vector{JLBoostTreeNode}
        JLBoostTreeNode(w) = new(w, JLBoostTreeNode[])
    end
end

I am doing this on JuliaBox and I don’t have access to v0.7 at work, and I think there might others with the same question, so posting here for the benefit of all.

5 Likes

new{typeof(w)}(...)

15 Likes

For the mentally one-legged like myself: the answer by @mohamed82008 works in this case because their struct is defined as

mutable struct JLBoostTreeNode{T <: Real} ... end

If your struct has more than one types, e.g.

struct MyType{N, T} ... end

Then naturally you will need to provide new with all types it is expecting.

8 Likes

There is this quote from the docs (Constructors · The Julia Language, end of the page):

So why isn’t the type resolved automatically here?