diff --git a/src/interface.jl b/src/interface.jl index c7c19df..e7c95c5 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 8c0fac7..3533263 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, copy] + @testset "1-ary $op" for op in [+, *, gcd, lcm, copy, abs] a = op(x) b = MA.operate(op, x) @test a == b