Julia 语法题

fn(x,y; a=1) = x+y+a

fn(x,y; a=1, b=1) = x+y+a+b

fn(1, 1; a = 1)

最后一行会打印什么?

julia关键词参数不参与多重分派,那么第二次fn函数定义会把第一次的定义给覆盖;
所以,fn(1, 1; a=1)是第二个函数,即4。

2 个赞

Julia quiz:

fn(x::Int, y) = x + y

fn(x, y::Int) = x + y

fn(1,1) # Error ... ambiguous

Which of these can disambiguate?

A) fn(x::T, y::S) where {T<:Integer, S<:T} = x + y

B) fn(x, @nospecialize(y::Int)) = x + y

C) fn(x::Int, y::Int) = x + y

D) All the above

#julialang

@nospecialize(y) 有何区别?

我也不懂。应该没有

感觉加不加**@nospecialize**对分派的使用上没有区别,是编译指令,我也没看懂?
但这道题应该选C