Constructors for parametric types

Julia doesn’t enforce this. As you say, the error comes from the type itself, before the constructor call even occurs. The constructor’s signature cannot widen the type’s parameters.

julia> struct X{T<:Real}
         function X{T}() where T<:Number
           return "hello"
         end
       end

julia> X{Int}()
"hello"

julia> X{Int}
X{Int64}

julia> X{Complex}
ERROR: TypeError: in X, in T, expected T<:Real, got Type{Complex}
2 Likes