Is it possible to have the same module imported twice with slightly different behaviour?

Say I want to overwrite the behaviour of a Module (eg. the rank function in LinearAlgebra always returns 0 (this is a fictional problem)), but I don’t want to brake other modules that use LinearAlgebra (eg. I want rank when called in DifferentialEquations to still work correctly). Is this possible?

using LinearAlgebra
const LinearAlgebra2 = deepcopy(LinearAlgebra) #doesn't work
LinearAlgera2.rank(::Matrix) = 0

Note: defining another function my_rank is not possible, because then the internal functions in LinearAlgebra2 like inv would still use rank instead of my_rank.

AFAIK, this is only possible by making a new Module LinearAlgebra2 and reusing all the code except rank.

The general solution here is to make your own type <: AbstractArray which has the behavior you want.

1 Like