Constructor for parametric type

Can some tell me why the last line does not work?
How can I add a constructor that does the same as the default constructor?
(i.e. Rulepath(1,v,true) works as expected)

struct Rulepath{T<:Unsigned}
    featid::Int64
    subset::Vector{T}
    isLeftChild::Bool    
end
v=UInt16[1,2,9]
Rulepath(1,v,true) #works

#however, I prefer to have different constructors:
struct Rulepath2{T<:Unsigned}
    featid::Int64
    subset::Vector{T}
    isLeftChild::Bool   
 function Rulepath2{T}() where T<:Unsigned
    return new(0,UInt8[],false)
  end
  function Rulepath2{T}(a,b::Vector{T},c)  where T<:Unsigned
    return new(a,b,c)
  end      
end

v=UInt16[1,2,9]
Rulepath2{UInt8}() #works

Rulepath2{eltype(v)}(1,v,true) #works

Rulepath2(1,v,true) #fails

The missing constructor is

Rulepath2(a,b::Vector{T},c)  where {T<:Unsigned} = Rulepath2{T}(a,b,c)

But are you sure about this part of your code?

Wouldn’t you want:

function Rulepath2{T}() where T<:Unsigned
    return new(0,T[],false)
end

But I think it is easier to accomplish what I assume you want by defining outer constructors for Rulepath.

julia> struct Rulepath{T<:Unsigned}
           featid::Int64
           subset::Vector{T}
           isLeftChild::Bool
       end

julia> Rulepath{T}() where {T<:Unsigned} = Rulepath(0,T[],false)

julia> v=UInt16[1,2,9]
3-element Array{UInt16,1}:
 0x0001
 0x0002
 0x0009

julia> Rulepath{UInt8}() #works
Rulepath{UInt8}(0, UInt8[], false)

julia> Rulepath{eltype(v)}(1,v,true) #works
Rulepath{UInt16}(1, UInt16[0x0001, 0x0002, 0x0009], true)

julia> Rulepath(1,v,true) #works
Rulepath{UInt16}(1, UInt16[0x0001, 0x0002, 0x0009], true)

2 Likes

Thank you!

And yes, indeed my constructor with UInt8 was silly.