A function can receive an arg of the type `=> A`

as in

```
def add1(x: => Int): Int = {
x + 1
}
```

But how do you return such a type in a function? The answer is that you can’t. But you can come up with something similar:

```
case class Lazy[T](f: () => T) extends (() => T){
lazy val v = f()
def apply(): T = v
}
def add1(x: Lazy[Int]): Int = {
x() + 1
}
add1(Lazy(() => 5))
```

## 0 comments:

Post a Comment