# Parametric constructor

**URL:** https://discourse.julialang.org/t/parametric-constructor/9290
**Category:** General Usage
**Created:** [February 24, 2018, 1:35am UTC](https://discourse.julialang.org/t/parametric-constructor/9290 "2018-02-24T01:35:22Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Snir\_Gazit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/snir_gazit/32/11027_2.png) [@Snir\_Gazit](https://discourse.julialang.org/u/Snir_Gazit)
#### Post date: [February 24, 2018, 1:35am UTC](https://discourse.julialang.org/t/parametric-constructor/9290/1 "2018-02-24T01:35:22Z")

</div>

I am defining the data structure below, which contains a string and a parameterized type array. I’m having trouble defining the corresponding parameterized constructor that initializes a zero-length array of the requested type. see below

```julia
type mydata{T}
    name::String
    data::Array{T,1}
end

function mydata{T}(name::String)
     mydata{T}(name, T[])
end

mydata{Int64}("myIntdata")

```

I am getting a warning “WARNING: static parameter T does not occur in signature for Type”, and consequently a call to the constructor is ignored with an error message.  
I am clearly defining the constructor in a wrong way…

---

<div class="post-metadata">

### Author: ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)
#### Post date: [February 24, 2018, 4:22am UTC](https://discourse.julialang.org/t/parametric-constructor/9290/2 "2018-02-24T04:22:52Z")

</div>

```julia
mutable struct MyData{T}
    name::String
    data::Vector{T}
    MyData{T}(obj::AbstractString) where T = new(obj, Vector{T}())
end
MyData{Int64}("X")

```

---

<div class="post-metadata">

### Author: ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)
#### Post date: [February 24, 2018, 4:53am UTC](https://discourse.julialang.org/t/parametric-constructor/9290/3 "2018-02-24T04:53:54Z")

</div>

Like this you lose the other constructor though: `MyData("a", [1,2])` no longer works.

I think it should be:

```julia
mutable struct MyData{T}
    name::String
    data::Vector{T}
end

(::Type{MyData{T}})(s::String) where {T} = MyData(s, T[])

MyData("a", [1,2])

MyData{Int64}("a")

```

---

<div class="post-metadata">

### Author: ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)
#### Post date: [February 24, 2018, 6:50am UTC](https://discourse.julialang.org/t/parametric-constructor/9290/4 "2018-02-24T06:50:57Z")

</div>

Use the newer syntax `where T`:

```julia
function mydata{T}(name::String) where T
     mydata{T}(name, T[])
end

```

The old syntax `function mydata{T}(name::String)` is equivalent to the new syntax `function mydata(name::String) where T`, and both correctly give an error.

There isn’t a consistent way to express what you want using the old syntax, and this was one of the motivating reasons for introducing the new syntax: [`where` in function definitions in julia-0.6 - Stack Overflow](https://stackoverflow.com/questions/41931289/where-in-function-definitions-in-julia-0-6)

---

<div class="post-metadata">

### Author: ![Snir\_Gazit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/snir_gazit/32/11027_2.png) [@Snir\_Gazit](https://discourse.julialang.org/u/Snir_Gazit)
#### Post date: [February 24, 2018, 5:21pm UTC](https://discourse.julialang.org/t/parametric-constructor/9290/5 "2018-02-24T17:21:31Z")

</div>

Thanks! this one works. I am still confused about the syntax though. Is there a difference between

```julia
(::Type{MyData{T}})

```

as opposed to just

```julia
MyData{T}

```

---

<div class="post-metadata">

### Author: ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)
#### Post date: [February 24, 2018, 6:02pm UTC](https://discourse.julialang.org/t/parametric-constructor/9290/6 "2018-02-24T18:02:34Z")

</div>

@greg_plowman has a cleaner solution. I somehow got convinced that the `Type` trick was necessary, but it’s not, so you should go with the `where` syntax as he explains above.

In Julia you can overload the function call operator: telling Julia that whenever there is a “function call” (something of the type `a(b)`) if `a` is of a given type, it should call a specific method. For example (don’t do this in your code!):

```julia
julia> (a::String)(x) = println("I can call strings!")

julia> "aa"(2)
I can call strings!

```

This is very powerful: for example if you have defined say a `struct` representing a polynomial, you could overload the function call so that `(p::Polynomial)(x::Number)` would compute the value of the polynomial at that number (see [this example](https://docs.julialang.org/en/stable/manual/methods/#Function-like-objects-1) from the docs).

I used an extra trick to say that I wanted to overload the function call only when my object _is_ `MyData{T}` for some `T`. To do that, you can dispatch on `Type{MyData{T}}` (`MyData{T}` are the only objects of type `Type{MyData{T}}` ).

As @greg_plowman points out, this is completely redundant and you may simply define your function with the `where` syntax, but at least you’ve learned one new trick 🙂
