2017-05-31 73 views
0

我有一個像下面這樣的函數,它由腳本自動生成。將「Any *」變量參數傳遞給需要「Any *」的另一個函數

def printFunc(args : Any*) : Unit = { 
    funcCallHandler.call(args) 
} 

funcCallHandler中的調用函數看起來像這樣。該函數也從其他生成的函數獲取可變參數。

def call(args : Any*) : Unit = { 

    for (arg <- args) { 
     /*Check type using a match function 
     and add do the rest of the code*/ 
    } 

} 

當我傳遞變量參數,我們從第一功能到了第二個提到的一個我把它作爲一個WrappedArray。有沒有一種方法來匹配這個WrappedArray或者有更好的方法來做這件事?

該類型沒有在第一個功能的情況下指定。

+3

[如何將scala數組傳遞到scala可變參數方法?](https://stackoverflow.com/questions/31064753/how-to-pass-scala-array-into-scala-vararg-method) –

+1

@DavisBroda'args'本身就是一個可變參數,這是一個不同的信息,可能會讓人困惑(儘管解決方案確實是一樣的)。 –

+0

'funcCallHandler.call(args:_ *)' – Dima

回答

2

來傳遞參數作爲可變參數,你需要做的

def printFunc(args : Any*) : Unit = { 
    funcCallHandler.call(args: _*) 
} 

否則,ARGS將被理解爲Seq[Any]類型的一個參數。

+0

非常感謝。這工作像一個魅力。 –

相關問題