# Constructor for parametric type

**URL:** <https://discourse.julialang.org/t/constructor-for-parametric-type/11758>\
**Category:** New to Julia\
**Created:** [June 18, 2018, 2:51pm UTC](https://discourse.julialang.org/t/constructor-for-parametric-type/11758 "2018-06-18T14:51:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bernhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhard/32/2619_2.png) [@bernhard](https://discourse.julialang.org/u/bernhard)\
**Post date:** [June 18, 2018, 2:51pm UTC](https://discourse.julialang.org/t/constructor-for-parametric-type/11758/1 "2018-06-18T14:51:30Z")

</div>

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)

```julia
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

```

---

<div class="post-metadata">

**Author:** ![favba](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/favba/32/2735_2.png) [@favba](https://discourse.julialang.org/u/favba)\
**Post date:** [June 18, 2018, 3:19pm UTC](https://discourse.julialang.org/t/constructor-for-parametric-type/11758/2 "2018-06-18T15:19:31Z")

</div>

The missing constructor is

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

```

But are you sure about this part of your code?

> [@bernhard](#):
>
> function Rulepath2{T}() where T\<:Unsigned return new(0,UInt8,false) end

Wouldn’t you want:

```julia
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
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)

```

---

<div class="post-metadata">

**Author:** ![bernhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhard/32/2619_2.png) [@bernhard](https://discourse.julialang.org/u/bernhard)\
**Post date:** [June 18, 2018, 3:25pm UTC](https://discourse.julialang.org/t/constructor-for-parametric-type/11758/3 "2018-06-18T15:25:50Z")

</div>

Thank you!

And yes, indeed my constructor with UInt8 was silly.
