Inner constructor error


I’m trying to create a test inner constructor to get scalar fields from a vector input. I find the following code works if I don’t provide the statement “new()” at the end, but in that case the instantiated object only has the scalar value of c rather than all of a, b and c.

mutable struct TestStruct{T <: Real}
    a :: T
    b :: T
    c :: T

    function TestStruct(V :: Vector{T}) where T <: Real
        a :: T = V[1]
        b :: T = V[2]
        c :: T = V[3]

The error I get on running the above is

ERROR: syntax: too few type parameters specified in "new{...}" around /Users/vijay/Documents/JULIA_PROJECT_1/JL_FILES/TestStruct.jl:1

I followed the example in the Julia Reference manual under Case Study: Rational, but I’m still not clear what’s wrong with the code above.

TestStruct{T} is a parameteric type. Because of that you need to return new{T}(...) from the inner constructor.

Btw. you don’t need the :: T in the body of the inner constructor, because the elements of Vector{T} are already of type T.

Thanks for that lightning response, @fatteneder

It works fine with the “new{T}(…)” option! :grinning: