繁体   English   中英

创建一个抽象类型作为不同函数的包装器

[英]creating an abstract type as a wrapper for different functions

我正在尝试创建一个abstract type ,它是一个超类(称为Kernel )几个不同的函数,每个函数Kernel的子类——即

abstract type Kernel end

struct ExponentialKernel <: Kernel
    ExponentialKernel(rate::Number, x::Number) = exp(-1*rate*x)
end


struct GaussianKernel <: Kernel
    GaussianKernel(sigma::Number, x::Number) = (1.0/(sigma*sqrt(2*pi))) * exp((-1*x^2)/(2*sigma^2)
end

...

等等。

当我使用上面的内容时,它的工作原理是当我调用ExpKernel(a,b)时我得到了正确的值,但是当我尝试将这些 kernel 对象传递给不同的构造函数时,比如

mutable struct Model
    kernel::T where {T <: Kernel}    

    Model(kernel) = new(kernel) 
end

但是当我尝试调用Model(ExpKernel)时出现错误

ERROR: MethodError: Cannot `convert` an object of type Type{ExpKernel} to an object of type Kernel

我在这里缺少关于类型的东西吗?

这是你想要的?

julia> mutable struct Model
           kernel::DataType

           Model(k::Type{<:Kernel}) =
               isconcretetype(k) ? new(k) : throw(ArgumentError("concrete type required"))
       end

julia> Model(GaussianKernel)
Model(GaussianKernel)

julia> Model(1)
ERROR: MethodError: no method matching Model(::Int64)
Closest candidates are:
  Model(::Type{var"#s1"} where var"#s1"<:Kernel) at REPL[4]:4
Stacktrace:
 [1] top-level scope at REPL[6]:1

julia> Model(Kernel)
ERROR: ArgumentError: concrete type required
Stacktrace:
 [1] Model(::Type{Kernel}) at .\REPL[4]:4
 [2] top-level scope at REPL[7]:1

但是请注意,这不是 Julia 中的典型设计模式 - 通常您会使用https://docs.julialang.org/en/v1/manual/methods/#Function-like-objects-1模式来实现此类功能。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM