# Type inference in constructors

**URL:** https://discourse.julialang.org/t/type-inference-in-constructors/11592
**Category:** New to Julia
**Created:** [June 11, 2018, 5:44pm UTC](https://discourse.julialang.org/t/type-inference-in-constructors/11592 "2018-06-11T17:44:02Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Pavel\_Kalouguine](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pavel_kalouguine/32/7730_2.png) [@Pavel\_Kalouguine](https://discourse.julialang.org/u/Pavel_Kalouguine)
#### Post date: [June 11, 2018, 5:44pm UTC](https://discourse.julialang.org/t/type-inference-in-constructors/11592/1 "2018-06-11T17:44:02Z")

</div>

I am trying to create a type representing crystallographic space groups in arbitrary dimensions. The group operations are stored as static matrices of integers (plus eventually a static rational vector for non-symmorphic groups, but this part is not done yet). Here’s the code:

```julia
struct SpaceGroup{N, N2}
    e2i::Dict{SMatrix{N,N,Int,N2},Int}
    i2e::Vector{SMatrix{N,N,Int,N2}}

    function SpaceGroup{N,N2}(g::Vector{SMatrix{N,N,Int,N2}}) where {N,N2}
        sg=new(Dict{SMatrix{N,N,Int,N2},Int}(), Vector{SMatrix{N,N,Int,N2}}())
        # More initialization here...
        return sg
    end
end

```

The inner constructor takes a vector of static matrices (group generators), with explicitely supplied type parameters. The question is: is it possible to infer these parameters from the type of the constructor argument, instead of passing them explicitely as in the snippet below?:

```julia
g=[@SMatrix [0 1 0 0; 0 0 1 0; 0 0 0 1; -1 0 0 0]]
sg=SpaceGroup{4,16}(g) # The type of g fixes the values of N and N2, does it?

```

Thanks.

---

<div class="post-metadata">

### Author: ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)
#### Post date: [June 11, 2018, 6:05pm UTC](https://discourse.julialang.org/t/type-inference-in-constructors/11592/2 "2018-06-11T18:05:10Z")

</div>

```julia
julia> SpaceGroup(g::Vector{SMatrix{N,N,Int,N2}}) where {N,N2} = SpaceGroup{N,N2}(g)
SpaceGroup

julia> SpaceGroup(g)
SpaceGroup{4,16}(Dict{StaticArrays.SArray{Tuple{4,4},Int64,2,16},Int64}(), StaticArrays.SArray{Tuple{4,4},Int64,2,16}[])

```

---

<div class="post-metadata">

### Author: ![Pavel\_Kalouguine](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pavel_kalouguine/32/7730_2.png) [@Pavel\_Kalouguine](https://discourse.julialang.org/u/Pavel_Kalouguine)
#### Post date: [June 11, 2018, 6:28pm UTC](https://discourse.julialang.org/t/type-inference-in-constructors/11592/3 "2018-06-11T18:28:57Z")

</div>

Wow, that was quick! Thank you, this is exactly what I wanted.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [June 12, 2018, 6:52am UTC](https://discourse.julialang.org/t/type-inference-in-constructors/11592/4 "2018-06-12T06:52:52Z")

</div>

Also note [the section on parametric constructors in the manual](https://docs.julialang.org/en/latest/manual/constructors/#Parametric-Constructors-1), especially the example following

> This automatic provision of constructors is equivalent to the following explicit declaration:

---

<div class="post-metadata">

### Author: ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)
#### Post date: [June 12, 2018, 9:39am UTC](https://discourse.julialang.org/t/type-inference-in-constructors/11592/5 "2018-06-12T09:39:22Z")

</div>

> [@Tamas\_Papp](#):
>
> Also note [the section on parametric constructors in the manual](https://docs.julialang.org/en/latest/manual/constructors/#Parametric-Constructors-1), especially the example following
> 
> > This automatic provision of constructors is equivalent to the following explicit declaration:

To be honest, the section of parametric constructor could have been largely augmented to provide more elaboration and examples. I found the information in that section insufficient every time when I looked for something more than the very basic.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [June 12, 2018, 11:59am UTC](https://discourse.julialang.org/t/type-inference-in-constructors/11592/6 "2018-06-12T11:59:16Z")

</div>

> [@chobbes](#):
>
> section of parametric constructor could have been largely augmented to provide more elaboration and examples

I agree, the best way to resolve that is an issue or a PR.
