Extending function with new keyword argument fails

I ran into this problem while trying to extend stdlib’s cholesky with a new keyword argument. This is a minimal example.

import LinearAlgebra: cholesky
struct T end
cholesky(A::AbstractMatrix, ::Val{false} = Val(false); b::T) = println(b)
A = randn(3, 3)
cholesky(A, Val(false); b = T()) # fails to run this method

The method fails to run, even though Julia lists it as a possible candidate in the stack trace without any associated error. I got the same error on both 1.3.1 and 1.4. What is going wrong here?

The method

cholesky(A::Union{StridedMatrix,RealHermSymComplexHerm{<:Real,<:StridedMatrix}},
    ::Val{false}=Val(false); check::Bool = true)

defined in LinearAlgebra is more specific so it is run instead (keyword arguments do not participate in dispatch).

3 Likes

Thank you for your answer! I didn’t know keyword arguments don’t participate in dispatch. For posterity, here is the relevant excerpt in the docs (in the Methods section)

Keyword arguments behave quite differently from ordinary positional arguments. In particular, they do not participate in method dispatch. Methods are dispatched based only on positional arguments, with keyword arguments processed after the matching method is identified.

1 Like