This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Why can't a vararg also be a call by name param ?

3 replies
Maxime Lévesque
Joined: 2009-08-18,
User offline. Last seen 42 years 45 weeks ago.

Scalac does not like this mix :

  def funk[T](a: =>T*)

Are there any good reason why ?

Cheers

Max

Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
Re: Why can't a vararg also be a call by name param ?
To get a good error message:

scala> def f[T](a: (=> T)*) : List[T] = Nil
<console>:1: error: no by-name parameter type allowed here
       def f[T](a: (=> T)*) : List[T] = Nil

instead of

scala> def f[T](a: => T*) : List[T] = Nil 
<console>:1: error: ')' expected but identifier found.
       def f[T](a: => T*) : List[T] = Nil


2009/11/24 Maxime Lévesque <maxime [dot] levesque [at] gmail [dot] com>

Scalac does not like this mix :

  def funk[T](a: =>T*)

Are there any good reason why ?

Cheers

Max




--
Viktor Klang
| "A complex system that works is invariably
| found to have evolved from a simple system
| that worked." - John Gall

Blog: klangism.blogspot.com
Twttr: twitter.com/viktorklang
Code: github.com/viktorklang
extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: Why can't a vararg also be a call by name param ?

On Tue, Nov 24, 2009 at 09:54:04AM -0500, Maxime Lévesque wrote:
> Scalac does not like this mix :
>
> def funk[T](a: =>T*)
>
> Are there any good reason why ?

https://lampsvn.epfl.ch/trac/scala/ticket/237
"Parameters should be allowed as by-name and repeated"

(Opened 2 years ago, so don't hold your breath)

Johannes Rudolph
Joined: 2008-12-17,
User offline. Last seen 29 weeks 21 hours ago.
Re: Why can't a vararg also be a call by name param ?

2009/11/24 Viktor Klang :
> scala> def f[T](a: (=> T)*) : List[T] = Nil
> :1: error: no by-name parameter type allowed here
>        def f[T](a: (=> T)*) : List[T] = Nil
>
> instead of
>
> scala> def f[T](a: => T*) : List[T] = Nil
> :1: error: ')' expected but identifier found.
>        def f[T](a: => T*) : List[T] = Nil

I think the second case could be interesting as well... It would have
to be of type Function0[Seq[T]]. The question would be what the exact
semantics of such a call from the view of the call-site would be.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland