现在每个函数和闭包都有自己的类型。
这是否意味着现在可以向高阶函数提供更详细的信息,例如foo(bar :: Function{Float64}) = ...
,与 0.5 之前的版本相反,其中的类型bar
不能比 更具体Function
?
如果是这样,这样做的正确方法是什么?如果不是,除了编译器能够更好地优化生成的代码之外,这种变化的实际意义是什么?TIA。
现在每个函数和闭包都有自己的类型。
这是否意味着现在可以向高阶函数提供更详细的信息,例如foo(bar :: Function{Float64}) = ...
,与 0.5 之前的版本相反,其中的类型bar
不能比 更具体Function
?
如果是这样,这样做的正确方法是什么?如果不是,除了编译器能够更好地优化生成的代码之外,这种变化的实际意义是什么?TIA。
不是真的没有。我明白你的意思,我喜欢它,但这是不可能的。(当然现在不会,可能永远不会。也许有一天会使用特征。)
让我们看一个例子: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
函数的类型是 a #foo
,它是 的子类型Function
。请注意,这#
意味着这是一个生成的名称,您不能在编写代码时将哈希放在名称中,但 julia 编译器(松散地使用该术语)可以。
为什么它的超超类型不更具体,而不仅仅是功能?
那会是什么?Function{Int64}
还是Function{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
所以我们成功地限制dothing
了,只取 a#foo
作为它的论据。当你给它一个#bar
. 这并不是真的有用,因为该foo
函数是唯一的 type #foo
。
我们可以使用Union
:
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
接受 a#foo
或 a #bar
。任何一个功能都有效。
作为白名单,这具有有限的应用程序。