关于Julia语言写法含义


#1

`export ReLU

“”"
$(TYPEDEF)

Represents a ReLU operation.

p(x) is shorthand for relu(x) when p is an instance of
ReLU.
“”"
struct ReLU <: Layer
tightening_algorithm::Union{TighteningAlgorithm, Nothing}
end

ReLU() = ReLU(nothing)

Base.hash(a::ReLU, h::UInt) = hash(:ReLU, h)

function Base.show(io::IO, p::ReLU)
print(io, “ReLU()”)
end

(p::ReLU)(x::Array{<:Real}) = relu(x)
(p::ReLU)(x::Array{<:JuMPLinearType}) = (Memento.info(MIPVerify.LOGGER, “Applying $p …”); relu(x, nta = p.tightening_algorithm))
`
代码中的最后两行代码的写法的语义是什么


#2

一般我们给函数f添加方法的时候是这么写的

julia> f()=42
f (generic function with 1 method)

julia> typeof(f)
typeof(f)

f自己是typeof(f)类型的对象)

至于你的这两行代码可以理解为给ReLU类型的对象添加方法,也就是functor/callable的概念

julia> struct S
           x
       end

julia> (s::S)()=println("My value is $(s.x)")

julia> s1=S(1)
S(1)

julia> s2=S(2)
S(2)

julia> s1()
My value is 1

julia> s2()
My value is 2

我们可以查看某个类型的方法表,看看它具有哪些方法:

julia> typeof(f).name.mt
# 1 method for generic function "f":
[1] f() in Main at REPL[1]:1

julia> S.name.mt
# 1 method for generic function "(::S)":
[1] (s::S)() in Main at REPL[4]:1