Type inference in constructors


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:

struct SpaceGroup{N, 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

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?:

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?


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

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


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


Also note the section on parametric constructors in the manual, 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.


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