(这个问题应该通过对Stroustrup的引用来回答.)
能够请求指向最派生类的指针似乎非常有用,如下所示:
class Base { ... };
class DerivedA { ... };
class DerivedB { ... };
class Processor
{
public:
void Do(Base* b) {...}
void Do(DerivedA* d) {...}
void Do(DerivedB* d) {...}
};
list<Base*> things;
Processor p;
for(list<Base*>::iterator i=things.begin(), e=things.end(); i!=e; ++i)
{
p.Do(CAST_TO_MOST_DERIVED_CLASS(*i));
}
Run Code Online (Sandbox Code Playgroud)
但是c ++中没有提供这种机制.为什么?
更新,激励示例:
假设您没有Base和Derived and Processor,而是拥有:
class Fruit
class Apple : public Fruit
class Orange: public Fruit
class Eater
{
void Eat(Fruit* f) { ... }
void Eat(Apple* f) { Wash(f); ... }
void …Run Code Online (Sandbox Code Playgroud) 每个函数和闭包现在都有自己的类型.
这是否意味着现在可以向更高阶的函数提供更详细的信息,例如foo(bar :: Function{Float64}) = ...,与0.5之前相比,其中的类型bar不能更具体Function?
如果是这样,这样做的正确方法是什么?如果没有,除了编译器能够更好地优化生成的代码之外,这个变化的实际导入是什么?TIA.
在Julia中,fun(n::T) where T<:Integer当函数适用于所有子类型时,我经常看到代码编写Integer.但有时,我也看到fun(n::Integer),一些指南声称与上述相同,而其他人则认为效率较低,因为Julia并不专注于特定的子类型,除非明确提到子类型T.
后一种形式显然更方便,如果可能,我希望能够使用它,但这两种形式是否相同?如果没有,它们之间的实际差异是什么?
我正在编写一些软件,其中涉及一个数量的各种功能形式的库。我想利用 Julia 的多重分派,但想知道是否有更有效的方法来实现此过程。
例如,考虑一个包含以下两个函数的库
function firstfunction(x::Float64)
return 2*x
end
function secondfunction(x::Float64)
return x^2
end
Run Code Online (Sandbox Code Playgroud)
我还想实现多个调度方法,这些方法可以将这些函数形式应用于值向量或向量数组(矩阵)。我可以这样做
function firstfunction(x::Float64)
return 2*x
end
function firstfunction(xs::Vector{Float64})
f = similar(xs)
for i = 1:size(xs, 1)
f[i] = 2*xs[i]
end
return f
end
function firstfunction(xss::Matrix{Float64})
f = similar(xss)
for i = 1:size(xss, 1)
for j = 1:size(xss, 2)
f[i, j] = 2*xss[i, j]
end
return f
end
function secondfunction(x::Float64)
return x^2
end
function secondfunction(xs::Vector{Float64})
f = similar(xs)
for i = 1:size(xs, 1)
f[i] = xs[i]^2
end …Run Code Online (Sandbox Code Playgroud) 正如标题中提到的,我想使用multiple-dispatch来分配同一个struct的不同行为,通过Symbol来区分。该结构体可以构造如下:
\nstruct AbstractAlgorithm\n algorithmName::String\n algorithmSymbol::Symbol\n\n function AbstractAlgorithm(algorithmName::String)\n if algorithmName \xe2\x88\x89 ("Value Iteration", "Policy Iteration")\n error("Given algorithm $algorithmName not defined yet.")\n elseif algorithmName=="Value Iteration"\n new(algorithmName, Symbol("vIter"))\n elseif algorithmName=="Policy Iteration"\n new(algorithmName, Symbol("pIter"))\n end\n end \nend\nRun Code Online (Sandbox Code Playgroud)\n我想在函数中使用不同的符号来区分相同的结构,例如:
\nfunction A(a::AbstractAlgorithm with Symbol vIter) = do A1\nfunction A(a::AbstractAlgorithm with Symbol pIter) = do A2\nRun Code Online (Sandbox Code Playgroud)\n我应该如何使用多重调度来设计函数 A?
\n我将以最小的例子解释我的问题.假设我有三个文件:
A.jl
module A
export Atype, f
type Atype
end
f = function(x::Atype)
println("f called with A")
end
end #module
Run Code Online (Sandbox Code Playgroud)
B.jl
module B
export Btype, f
type Btype
end
f = function(x::Btype)
println("f called with B")
end
end #module
Run Code Online (Sandbox Code Playgroud)
Main.jl
using A
using B
main = function()
x = Atype()
f(x)
end
main()
Run Code Online (Sandbox Code Playgroud)
这里我有两个版本的f功能.如果我理解正确的多次发送的想法,应该在运行时期间扣除应该使用哪个版本.因此,我预计运行Main.jl会打印出来f called with A.不幸的是,我明白了
$ julia Main.jl
ERROR: type: anonymous: in typeassert, expected Btype, got Atype
in include at /usr/bin/../lib64/julia/sys.so
in process_options at …Run Code Online (Sandbox Code Playgroud) 根据朱莉娅手册的"性能提示"部分,不建议多次发送疯狂.我遇到了一种情况,我似乎需要为我定义的类型提供3个参数.这与我关于仅为2种可能类型使用1个参数的问题有关.我意识到我可以通过简单地使用另一个参数来解决那里出现的困难但是我的类型看起来像
type mytype{R<:Real, S<:Any, T<:Any}
x::Matrix{R}
y::Dict{Int64, Vector{S}}
z::Dict{T, Vector{Int64}}
end
Run Code Online (Sandbox Code Playgroud)
这是一个不可取的性能中断,有几个参数可以发送.mytype上的函数然后将调度3个参数,函数参数是否正确?
我正在尝试使用多个分派来重载和使用组合类中的方法。这是实现:
role A {
has $!b;
submethod BUILD( :$!b ) {}
multi method bar () {
return $!b;
}
}
class B does A {
submethod BUILD( :$!b ) {}
multi method bar() {
return " * " ~ callsame ~ " * ";
}
}
my $a = A.new( b => 33);
say $a.bar();
my $b = B.new( b => 33 );
say $b.bar();
Run Code Online (Sandbox Code Playgroud)
但是,此操作失败:
Calling callsame(Str) will never work with declared signature ()
Run Code Online (Sandbox Code Playgroud)
(我真的不知道为什么callame Str用作签名)。更改method bar …
Fortran 中的声明是否使其成为正式实现多重分派的INTERFACE编程语言?(我问这个问题是因为链接的维基百科文章在其看似全面的支持相关范式的示例编程语言列表中并未包含 Fortran)。
怎么可能使这段代码工作?
据我所知,Scala 没有动态调度(类似于 Java)。是否可以以某种方式模拟动态调度?
或者最好的解决办法是什么?
object Tezt {
case class SuperClazz()
case class SubClazz1() extends SuperClazz
case class SubClazz2() extends SuperClazz
def method(obj: SubClazz1) = {
// stuff
}
def method(obj: SubClazz2) = {
// stuff
}
def func[T <: SuperClazz](obj: T) = {
Tezt.method(obj) // Error: Cannot resolve method reference with such signature
}
}
Run Code Online (Sandbox Code Playgroud) types scala type-inference multiple-dispatch dynamic-dispatch