lazy varargs?

View: New views
3 Messages — Rating Filter:   Alert me  

lazy varargs?

by Landei :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi!

Just a small question: Wouldn't it make sense that varargs behave lazy? I mean, maybe just a few elements from the sequence might be actually needed inside the function. IMHO having them wrapped in a Stream instead of an Array could be very useful.

Cheers,
Daniel

Re: lazy varargs?

by Eric Willigers :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Landei wrote:
> Hi!
>
> Just a small question: Wouldn't it make sense that varargs behave lazy? I
> mean, maybe just a few elements from the sequence might be actually needed
> inside the function. IMHO having them wrapped in a Stream instead of an
> Array could be very useful.

Stream wouldn't make the first element lazy.


Note you can sometimes "cheat":-


class Wrapper(expression: => Int) {
     def evaluate = expression
}

object Test {

     implicit def int2Wrapper(expression: => Int) = new Wrapper(expression)

     def f(seq: Wrapper*) {
         println(seq(0).evaluate)
     }

     def main(args: Array[String]) {
         f(32, 1/0)
     }

}

32


Re: lazy varargs?

by Landei :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Eric Willigers wrote:
Landei wrote:
> Hi!
>
> Just a small question: Wouldn't it make sense that varargs behave lazy? I
> mean, maybe just a few elements from the sequence might be actually needed
> inside the function. IMHO having them wrapped in a Stream instead of an
> Array could be very useful.

Stream wouldn't make the first element lazy.


Note you can sometimes "cheat":-


class Wrapper(expression: => Int) {
     def evaluate = expression
}

object Test {

     implicit def int2Wrapper(expression: => Int) = new Wrapper(expression)

     def f(seq: Wrapper*) {
         println(seq(0).evaluate)
     }

     def main(args: Array[String]) {
         f(32, 1/0)
     }

}

32
Thanks, that looks interesting. So maybe it would be a good idea to generalize this technique and have a generic lazy Wrapper class (LazyCell?) and an implicit conversion as part of the standard lib? I think this could be very useful, not only for varargs.

Cheers,
Daniel