`f(x::T, y::T)::T where {T}` 为什么 `T not defined`

在 Julia 1.0.0 跑如下的程序:


为什么第一种语法就没有报错,第二种语法(加了一个返回类型)就报错了呢?

julia> f(x::T, y::T)::T where {T} = x + y

(f(x::T, y::T)::T) where {T} = x + y

看起来像个 bug

1 个赞

写错了f(x::T, y::T)::T,跑到函数外面去了

不是bug

julia> function f(a::T, b::T)::T where T
           a+b
       end
f (generic function with 1 method)

或者

julia> (f(a::T, b::T)::T) where T = a+b
f (generic function with 1 method)

因为

julia> :(f(a::T, b::T)::T where T)
:(f(a::T, b::T)::(T where T))

julia> :((f(a::T, b::T)::T) where T)
:((f(a::T, b::T)::T) where T)

我记得Julia以前有个issue就是关于这个的。开发者直接说加个括号不是那么难,就关了。(好像理论上也没办法parse没有括号的one line function definition)

3 个赞