diff --git a/src/interface.jl b/src/interface.jl index 5b3452ee..c825ddb2 100644 --- a/src/interface.jl +++ b/src/interface.jl @@ -190,6 +190,10 @@ function operate end # custom `operate` method. operate(::typeof(-), x) = -x +# This is not efficient, as it may make unnecessary intermediate copies, +# but seems like the best way to write a generic implementation. +operate(::typeof(abs), x) = abs(copy_if_mutable(x)) + function operate( op::Union{ typeof(+), diff --git a/test/interface.jl b/test/interface.jl index 0f3c4e3c..9e3e654b 100644 --- a/test/interface.jl +++ b/test/interface.jl @@ -97,7 +97,7 @@ end @testset "operate" begin @testset "$T" for T in (Int, BigInt, Rational{Int}) x = T(7) - @testset "1-ary $op" for op in [+, *, gcd, lcm] + @testset "1-ary $op" for op in [+, *, gcd, lcm, abs] a = op(x) b = MA.operate(op, x) @test a == b