我有一個重載的方法定義如下:斯卡拉超載高階函數會導致錯誤類型
def g(f:() ⇒ Double): Object = null
def g(f: Double ⇒ Double): Object = null
def g(f: (Double, Double) ⇒ Double): Object = null
def h(f: (Double, Double) ⇒ Double): Object = null
雖然h(math.max _)
作品如預期,稱g(math.max _)
給我的錯誤「重載方法......不能應用到((Int,Int)=> Int)「。看來編譯器選擇了Int版本math.max
而不是Double版本。
如何以math.max作爲參數調用我的重載方法g
?如果沒有下劃線,我可以致電g(math.max)
,這將是額外的好處。
正如Rex指出的,'max'是一種重載方法,有四種選擇。顯然只有最大值(Double,Double)有效,但推理者不夠聰明。 – pedrofurla