parameter list (“*”) with lazy “by-name” parameters?

岁酱吖の 提交于 2019-12-06 09:57:42

Here the => means that the parameter is passed by name. What you have tried isn't allowed simply because =>String is not an actual type (as opposed to say ()=>String) and you cannot make an Array[=>String]. Given that a variadic parameter x: T* is under the hood handled as an array containing all the parameter's values (as in x: Array[T]), this is the reason why not being able to create a Array[=>String] (which does not make sense) also means that a variadic parameter f: (=>String)* is not possible.

This problem can be worked around using a little wrapper class:

implicit class ByName[T]( getValue: => T ) extends Proxy {
  def apply(): T = getValue
  def self = apply()
}

Then change the signature of your method like so:

def foo( fs: ByName[String]* )

When calling your method with multiple arguments, all the arguments will be implicitly wrapped into a ByName instance, and then you can call apply to get the actal value:

def foo( fs: ByName[String]* ) = fs foreach { f => println( f() ) }

Given that ByName extends Proxy for simple things such as calling toString or testing equality you don't even have to call apply. Thus you can simply do:

def foo( fs: ByName[String]* ) = f foreach println
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!