Reputation: 2574
I want to do this, the parameter is lazy and repeatable:
def f(actions: (=> Try[String])*) = {
actions.map{x =>
if(x.isFailure) throw new Exception()
...
}
}
So, I can evaluate parameter with this:
f(Try("a"), Try("b"), Try[String](new Exception()), Try({print("something"); "d"}))
The print("something") never be executed because parameters is lazy.
rather then:
def f(actions: (() => Try[String])*) = ???
f(() => Try("a"),() => Try("b"),() => Try[String](new Exception()), () => Try({print("something"); "d"}))
It just writing feeling boring.
Is Scala support the first one?
Upvotes: 0
Views: 101
Reputation: 29193
Wrap by name parameters as so:
implicit class ByNameWrapper[+A](a: => A) { def get: A = a }
And define your method as
def f(actions: ByNameWrapper[Try[String]]*) {
...
}
Usage is the same as normal by-name parameters:
f(Try { throw new Exception }, Try { println("a"); "b" })
Upvotes: 3
Reputation: 39587
It's coming to dotty. Or it's already in dotty.
https://github.com/lampepfl/dotty/issues/499
Upvotes: 1