Is there a way to creat a function, which can be called on the right of arguments?

A = [[1,2,3] [2,3,4]
#I can call adjoint like this
A'

So, is there way I can do this?

⁻¹(x::Matrix) = inv(x)

(A' * A)⁻¹

No, there’s no overloadable postfix operators.

1 Like

Yes, there are. But they are a hard coded set of symbols.

julia> Meta.@lower M'
:($(Expr(:thunk, CodeInfo(
    @ none within `top-level scope`
1 ─ %1 = var"'"(M)
└──      return %1
))))

I think technically ' is a postfix operator?

but:

julia> var"'"
adjoint (generic function with 47 methods)
3 Likes

OK, I was wrong then.

' seems to be the only postfix operator then (except for things like ..., which is an operator in a different sense):

julia> struct S
         n::Float64
         m::Float64
       end

julia> Base.adjoint(s::S) = S(s.m, s.n)

julia> s = S(3, 7)
S(3.0, 7.0)

julia> s'
S(7.0, 3.0)

It’s identified with Base.adjoint here:

N.B: many other operators are directly overloadable, e.g. this works:

julia> Base.:(+)(a::S, b::S) = S(a.n+b.n, a.m+b.m)

julia> S(10, 20) + S(9, 2)
S(19.0, 22.0)

Relevant documentation: Modules · The Julia Language

well

julia> struct S end

julia> var"'"
adjoint (generic function with 47 methods)

julia> Base.var"'"(x::S) = 3

julia> var"'"
adjoint (generic function with 48 methods)

julia> S()'
3
4 Likes