Overwrittng constant parameter restriction in method definition

Following the question asked here Propagate Type Parameters in Methods for Julia - Stack Overflow.

If you define a constant like

const RT{R<:Real} = Type{R}

then defining a method using this constant without a parameter

rt(r::RT) = r

keeps the type restriction, which can be seen by inspecting the defined method:

julia> methods(rt)
# 1 method for generic function "rt":
[1] rt(::Type{R} where R<:Real) in Main at REPL[16]:1

However, if you can overwrite this restriction when defining a method like this:

julia> methods(rt2)
# 1 method for generic function "rt2":
[1] rt2(::Type{R}) where R<:AbstractString in Main at REPL[29]:1

julia> rt3(::RT{R} where {R <: AbstractString}) = R
rt3 (generic function with 1 method)

julia> methods(rt3)
# 1 method for generic function "rt3":
[1] rt3(::Type{R} where R<:AbstractString) in Main at REPL[31]:1

Is this behavior indented?

I would say it is intended, but not desired :slight_smile: See issue https://github.com/JuliaLang/julia/issues/6383. I do plan to address this somehow eventually. The tricky part is that we can’t really compare types with free variables, so it’s not clear what the rules should be. For example say we have

const RT{T<:AbstractArray} = Vector{T}

Then a constraint like RT{S} where S<:Array{T} where ... is probably valid, but RT{S} where S<:AbstractDict where ... probably isn’t.

3 Likes