Scala variable argument list with call-by-name possible?

前端 未结 2 759
春和景丽
春和景丽 2020-12-03 12:38

I\'ve got some code like this:

def foo (s: => Any) = println(s)

But when I want to transform this to an argument list with variable leng

2条回答
  •  有刺的猬
    2020-12-03 12:48

    You have to use zero-argument functions instead. If you want, you can

    implicit def byname_to_noarg[A](a: => A) = () => a
    

    and then

    def foo(s: (() => Any)*) = s.foreach(a => println(a()))
    
    scala> foo("fish", Some(7), {println("This still happens first"); true })
    This still happens first
    fish
    Some(7)
    true
    

提交回复
热议问题