Surprising StackOverflowError

I encountered a surprising error today. The following two lines result in a StackOverflowError (with Julia 1.4.2):

f(x::T, y::T, z::S) where {S,T<:Vector{S}} = "method 1"
f(x::T, z::S, y::T) where {S,T<:Vector{S}} = "method 2"

I am struggling understanding why this is not allowed. Does anyone have an explanation? :slight_smile:

EDIT: I should mention that

f(x::Vector{S}, y::Vector{S}, z::S) where {S} = "method 1"
f(x::Vector{S}, z::S, y::Vector{S}) where {S} = "method 2"

works properly.

Seems to be some kind of bug:

julia> f(x::T, y::T, z::S) where {S,T<:Vector{S}} = "method 1"
f (generic function with 1 method)

julia> f(x::T, z::S, y::T) where {S,T<:Vector{S}} = "method 2"
ERROR: StackOverflowError:
Stacktrace:
 [1] top-level scope at REPL[2]:1

julia> methods(f)
# 2 methods for generic function "f":
[1] f(x::T, y::T, z::S) where {S, T<:Array{S,1}} in Main at REPL[1]:1
[2] f(x::T, z::S, y::T) where {S, T<:Array{S,1}} in Main at REPL[2]:1

julia> f([1,2],1,[1,2])
"method 2"

julia> f([1,2],[1,2],1)
"method 1"

It happens in 1.5.0-rc1 too.

1 Like

I can confirm this on current master. Cf

which may be related (but it is worth asking there with the example above).

1 Like