Parametric structures and Inner constructors that don't depend on the types

Hi all, I apologize for the title which is not clear at all, but I don’t know how to properly express my need concisely.

I currently try to define a parametric struct for which the inner constructor doesn’t depend on the types defined when declaring the fields.

Here is a MWE

struct MyStruct{T <: AbstractRange}
   t :: T

   function Mystruct(a) where T
      t = a:3a
      return new{T}(t)
   end
end

When I create an instance of MyStruct such as ms = MyStruct(2), I receive the following error:

UndefVarError: `T` not defined in static parameter matching
Suggestion: run Test.detect_unbound_args to detect method arguments that do not fully constrain a type parameter.

While I understand why the error is raised, I have no idea how to solve it.

Do you have any tips ?

Thank you

struct MyStruct{T <: AbstractRange}
    t :: T

    function MyStruct(a)
        t = a:3a
        return new{typeof(t)}(t)
    end
end
julia> MyStruct(5)
MyStruct{UnitRange{Int64}}(5:15)
2 Likes

A big thank you @barucden

1 Like