# Constructor with a vector of parameters doesn't seem to be recognized?

**URL:** <https://discourse.julialang.org/t/constructor-with-a-vector-of-parameters-doesnt-seem-to-be-recognized/23785>\
**Category:** New to Julia\
**Created:** [May 2, 2019, 5:44pm UTC](https://discourse.julialang.org/t/constructor-with-a-vector-of-parameters-doesnt-seem-to-be-recognized/23785 "2019-05-02T17:44:09Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![doomphoenix-qxz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/doomphoenix-qxz/32/7009_2.png) [@doomphoenix-qxz](https://discourse.julialang.org/u/doomphoenix-qxz)\
**Post date:** [May 2, 2019, 5:44pm UTC](https://discourse.julialang.org/t/constructor-with-a-vector-of-parameters-doesnt-seem-to-be-recognized/23785/1 "2019-05-02T17:44:09Z")

</div>

Minimal working example:

```julia
struct PureComponentData{Q, T}

    ΔGf::T
    ΔHf::T
    ΔSf::T
    ΔGm::T
    ΔHm::T
    ΔSm::T 
    Tm::T
    Cp::Q
end 

struct CpData{T}
    Tmin::T
    Tmax::T
    a::T
    b::T
    c::T
    d::T
end

function PureComponentData{Q,T}(params::Vector{T}, Cp::Q) where {Q,T}
    if length(params) != 7
        throw(ArgumentError("Must supply the correct number of parameters for Component Data"))
    end
    return PureComponentData(params..., Cp)
end

function __main__ ()
    Cp = CpData(0,0,0,0,0,0)
    Compound = PureComponentData([1,1,1,1,1,1], Cp)
end

```

Running this file, I get:

```julia
julia> __main__ ()
ERROR: MethodError: no method matching PureComponentData(::Array{Int64,1}, ::CpData1{Int64})
Closest candidates are:
  PureComponentData(::T, ::T, ::T, ::T, ::T, ::T, ::T, ::Q) where {Q, T} at /path/to/file.jl:3
Stacktrace:
 [1] __main__ () at /path/to/file.jl:31
 [2] top-level scope at none:0

```

Why isn’t Julia recognizing the alternate constructor? What should I do instead?

---

<div class="post-metadata">

**Author:** ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)\
**Post date:** [May 2, 2019, 6:01pm UTC](https://discourse.julialang.org/t/constructor-with-a-vector-of-parameters-doesnt-seem-to-be-recognized/23785/2 "2019-05-02T18:01:05Z")

</div>

The constructor signature isn’t correct. Try this:

```julia
function PureComponentData(params::Vector{T}, Cp::CpData{Q}) where {Q,T}
    if length(params) != 7
        throw(ArgumentError("Must supply the correct number of parameters for Component Data"))
    end
    return PureComponentData(params..., Cp)
end

```

---

<div class="post-metadata">

**Author:** ![doomphoenix-qxz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/doomphoenix-qxz/32/7009_2.png) [@doomphoenix-qxz](https://discourse.julialang.org/u/doomphoenix-qxz)\
**Post date:** [May 2, 2019, 7:52pm UTC](https://discourse.julialang.org/t/constructor-with-a-vector-of-parameters-doesnt-seem-to-be-recognized/23785/3 "2019-05-02T19:52:21Z")

</div>

Thanks for your answer! I was hopeful, but the error persists after making that change. Did you test it? Any other ideas?

---

<div class="post-metadata">

**Author:** ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)\
**Post date:** [May 2, 2019, 7:55pm UTC](https://discourse.julialang.org/t/constructor-with-a-vector-of-parameters-doesnt-seem-to-be-recognized/23785/4 "2019-05-02T19:55:59Z")

</div>

> [@doomphoenix-qxz](#):
>
> Did you test it?

Yes. Perhaps you can restart REPL and try again? Just post your new findings here.

---

<div class="post-metadata">

**Author:** ![doomphoenix-qxz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/doomphoenix-qxz/32/7009_2.png) [@doomphoenix-qxz](https://discourse.julialang.org/u/doomphoenix-qxz)\
**Post date:** [May 2, 2019, 8:08pm UTC](https://discourse.julialang.org/t/constructor-with-a-vector-of-parameters-doesnt-seem-to-be-recognized/23785/5 "2019-05-02T20:08:30Z")

</div>

I’m still getting the same error. This is the file I’m running now:

```julia
struct PureComponentData{Q, T}

    ΔGf::T
    ΔHf::T
    ΔSf::T
    ΔGm::T
    ΔHm::T
    ΔSm::T 
    Tm::T
    Cp::Q
end 

struct CpData{T}
    Tmin::T
    Tmax::T
    a::T
    b::T
    c::T
    d::T
end

function PureComponentData(params::Vector{T}, Cp::CpData{Q}) where {Q,T}
    if length(params) != 7
        throw(ArgumentError("Must supply the correct number of parameters for Component Data"))
    end
    return PureComponentData(params..., Cp)
end
function __main__ ()
    Cp = CpData(0,0,0,0,0,0)
    Compound = PureComponentData([1,1,1,1,1,1], Cp)
end

```

And it gives the same error:

```julia
               _
   _ _ _(_)_ | Documentation: https://docs.julialang.org
  (_) | (_) (_) |
   _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` | |
  | | |_| | | | (_| | | Version 1.1.0 (2019-01-21)
 _/ |\ __'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ `

julia> include("mwe.jl")
__main__ (generic function with 1 method)

julia> __main__ ()
ERROR: MethodError: no method matching PureComponentData(::Array{Int64,1}, ::CpData1{Int64})
Closest candidates are:
  PureComponentData(::T, ::T, ::T, ::T, ::T, ::T, ::T, ::Q) where {Q, T} at /path/to/mwe.jl:3
Stacktrace:
 [1] __main__ () at /path/to/mwe.jl:3:31
 [2] top-level scope at none:0

```

---

<div class="post-metadata">

**Author:** ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)\
**Post date:** [May 2, 2019, 8:14pm UTC](https://discourse.julialang.org/t/constructor-with-a-vector-of-parameters-doesnt-seem-to-be-recognized/23785/6 "2019-05-02T20:14:03Z")

</div>

> [@doomphoenix-qxz](#):
>
> ERROR: MethodError: no method matching PureComponentData(::Array{Int64,1}, ::CpData1{Int64})

I copied and paste your mwe.jl content and ran it without any problem. However, I’m curious why you have `CpData1` instead of `CpData` in the error message?

---

<div class="post-metadata">

**Author:** ![doomphoenix-qxz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/doomphoenix-qxz/32/7009_2.png) [@doomphoenix-qxz](https://discourse.julialang.org/u/doomphoenix-qxz)\
**Post date:** [May 2, 2019, 8:18pm UTC](https://discourse.julialang.org/t/constructor-with-a-vector-of-parameters-doesnt-seem-to-be-recognized/23785/7 "2019-05-02T20:18:47Z")

</div>

… Well. D’Oh. I had the name `CpData1` in the original code, which helped me figure out the reason the updated version wasn’t working correctly. I hadn’t actually saved the file after changing it. Now it does indeed work great!

---

<div class="post-metadata">

**Author:** ![doomphoenix-qxz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/doomphoenix-qxz/32/7009_2.png) [@doomphoenix-qxz](https://discourse.julialang.org/u/doomphoenix-qxz)\
**Post date:** [May 2, 2019, 8:51pm UTC](https://discourse.julialang.org/t/constructor-with-a-vector-of-parameters-doesnt-seem-to-be-recognized/23785/8 "2019-05-02T20:51:03Z")

</div>

I have one final question here. So in general then, for some type `MyStruct{T, U}`:

```julia
struct MyStruct{T,U}
    x::T 
    y::U 
end 

```

Is it true that you can’t define some outer constructor  
`MyStruct{T,U}(x::T, y::U) = MyStruct(x,y)` and then call it with some `y` that is itself a parametric type? If so, why not?

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [May 2, 2019, 9:45pm UTC](https://discourse.julialang.org/t/constructor-with-a-vector-of-parameters-doesnt-seem-to-be-recognized/23785/9 "2019-05-02T21:45:29Z")

</div>

No, whether or not `y` has a parametric type is of no significance. An outer constructor of the form:

```julia
Foo{T}(...) where {T} = ...

```

is only invoked if you actually call it with some `{T}` at the call site (as in `f = Foo{Int}(...)` not `f = Foo(...)`). That’s the only reason your initial code didn’t work. If you want an outer constructor that doesn’t require the {T} to be specified, then you would define it as

```julia
Foo(...) where {...} = ...

```

---

<div class="post-metadata">

**Author:** ![doomphoenix-qxz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/doomphoenix-qxz/32/7009_2.png) [@doomphoenix-qxz](https://discourse.julialang.org/u/doomphoenix-qxz)\
**Post date:** [May 2, 2019, 10:01pm UTC](https://discourse.julialang.org/t/constructor-with-a-vector-of-parameters-doesnt-seem-to-be-recognized/23785/10 "2019-05-02T22:01:06Z")

</div>

Ooooooohhh. Thank you. I read through the docs on constructors and didn’t understand that at all. Perhaps that point should be added to the manual, or clarified.
