# Constructors for different type parameters?

**URL:** <https://discourse.julialang.org/t/constructors-for-different-type-parameters/18867>\
**Category:** General Usage\
**Created:** [December 20, 2018, 7:53pm UTC](https://discourse.julialang.org/t/constructors-for-different-type-parameters/18867 "2018-12-20T19:53:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ArjunNarayanan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/arjunnarayanan/32/6475_2.png) [@ArjunNarayanan](https://discourse.julialang.org/u/ArjunNarayanan)\
**Post date:** [December 20, 2018, 7:53pm UTC](https://discourse.julialang.org/t/constructors-for-different-type-parameters/18867/1 "2018-12-20T19:53:04Z")

</div>

Is there a way to write a constructor for a type that has part of its parameter list specified?

For example, consider `struct MyType{A,B} end`. I would like to write a constructor for the type `MyType{Int,B} where B` i.e. where one of my type parameters is already known.

I’ve tried the following (and various different combinations):

```julia
struct MyType{A,B}
function MyType{Int, B}(x::B) where B
    new()
end
end

```

When I try to create an object of `MyType`, I get this:

```julia
julia> MyType{Int}(1)
ERROR: MethodError: no method matching MyType{Int64,B} where B(::Int64)

```

The strange thing is if I try to define a constructor with form similar to what crops up in the error message (i.e. something like `function MyType{Int, B} where B(x::B) new() end` I get a syntax error.

I’ve tried looking for related questions and I think this [Defining a constructor for partially parameterized type](https://discourse.julialang.org/t/defining-a-constructor-for-partially-parameterized-type/1307) is quite close to what I am looking for, but I can’t quite adapt it to my case.

Any suggestions will be helpful! 🙂

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [December 20, 2018, 8:01pm UTC](https://discourse.julialang.org/t/constructors-for-different-type-parameters/18867/2 "2018-12-20T20:01:16Z")

</div>

Are you looking for something like this?

```julia
julia> struct MyType{A,B}        
         MyType{Int}(x) = new{Int,typeof(x)}() 
       end

julia> MyType{Int}(3)
MyType{Int64,Int64}()

julia> MyType{Int}(3.)
MyType{Int64,Float64}()                   

```

---

<div class="post-metadata">

**Author:** ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)\
**Post date:** [December 20, 2018, 8:05pm UTC](https://discourse.julialang.org/t/constructors-for-different-type-parameters/18867/3 "2018-12-20T20:05:48Z")

</div>

Or more general

```julia
julia> struct MyType{A,B} end
julia> MyType{X}(y) where X = MyType{X,typeof(y)}()

julia> MyType{Float64}(1)
MyType{Float64,Int64}()

```

---

<div class="post-metadata">

**Author:** ![ArjunNarayanan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/arjunnarayanan/32/6475_2.png) [@ArjunNarayanan](https://discourse.julialang.org/u/ArjunNarayanan)\
**Post date:** [December 21, 2018, 1:39am UTC](https://discourse.julialang.org/t/constructors-for-different-type-parameters/18867/4 "2018-12-21T01:39:05Z")

</div>

Yes! That is exactly what I wanted. Thank you very much 😄
