Function Julia v0.5+中函数的调度;

Function Julia v0.5+中函数的调度;,function,types,julia,multiple-dispatch,Function,Types,Julia,Multiple Dispatch,据报道, 每个函数和闭包现在都有自己的类型 这是否意味着现在可以为高阶函数提供更详细的信息,例如foo(bar::function{Float64})=……,而不是0.5之前的版本,在0.5之前的版本中,bar的类型不能比function更具体 如果是,正确的做法是什么?如果没有,除了编译器能够更好地优化生成的代码之外,这个更改的实际意义是什么?TIA。不太可能。 我明白你的意思,我喜欢,但这是不可能的。 (目前肯定不会,可能永远也不会。也许有一天会使用traits。) 让我们看一个例子:foo

据报道,

每个函数和闭包现在都有自己的类型

这是否意味着现在可以为高阶函数提供更详细的信息,例如
foo(bar::function{Float64})=……
,而不是0.5之前的版本,在0.5之前的版本中,
bar
的类型不能比
function
更具体

如果是,正确的做法是什么?如果没有,除了编译器能够更好地优化生成的代码之外,这个更改的实际意义是什么?TIA。

不太可能。 我明白你的意思,我喜欢,但这是不可能的。 (目前肯定不会,可能永远也不会。也许有一天会使用traits。)

让我们看一个例子:
foo
bar

julia> foo(x::String) = println(x)
foo (generic function with 1 method)

julia> foo(x::Int64) = println(x+1)
foo (generic function with 2 methods)

julia> bar(x...) = println(x)
bar (generic function with 1 method)
foo
的类型层次结构是什么

julia> typeof(foo)
#foo

julia> supertype(typeof(foo))
Function

julia> supertype(supertype(typeof(foo)))
Any
所以我们看到
foo
函数的类型是
#foo
,它是
函数的子类型。请注意,
#
意味着这是一个生成的名称,在编写代码时不能在名称中放入散列,但julia编译器(松散地使用术语)可以

为什么它的超超类型不比功能更具体? 会是什么<代码>函数{Int64}
函数{String}
julia中的函数没有类型签名,方法有。 函数只是多次分派的名称,方法实际上是分派到的对象。粗略地说,函数名表示我应该在哪个表中查找,参数的类型(即它的类型签名)是在该表中查找的关键。方法本身就是使用该键返回的

让我们继续我们的例子,看看我们能做些什么:

julia> dothing(f::typeof(foo)) = f(rand([randstring(), rand(Int64)]))
dothing (generic function with 1 method)

julia> dothing(foo)
3139374763834167054

julia> dothing(foo)
Ed2kNGrd


julia> dothing(bar)
ERROR: MethodError: no method matching dothing(::#bar)
Closest candidates are:
  dothing(::#foo) at REPL[11]:1
因此,我们成功地限制了
doting
,仅将
#foo
作为其论点。查看当您给它一个
#条时,它会抛出一个错误。
这并不是很有用,因为
foo
函数是唯一的
#foo
类型

我们可以使用
联合
,不过:

julia> dootherthing(f::Union{typeof(foo),typeof(bar)}) = f(rand([randstring(), rand(Int64)]))
dootherthing (generic function with 1 method)

julia> dootherthing(foo)
9107791406050657562

julia> dootherthing(foo)
SmB2Xmw8

julia> dootherthing(bar)
("1IpZIMnx",)

julia> dootherthing(bar)
(-6356894350805213697,)


julia> dootherthing(str)
ERROR: UndefVarError: str not defined

julia> dootherthing(string)
ERROR: MethodError: no method matching dootherthing(::Base.#string)
Closest candidates are:
  dootherthing(::Union{#bar,#foo}) at REPL[19]:1
dootherthing
接受
#foo
#bar
。 这两种功能都可以工作


作为白名单,此应用程序有限。

谢谢。在试验了
typeof
之后,我也怀疑这一点。结果证明,这个问题有一个部分的解决方案,即。