Hacker News new | ask | show | jobs
by AzzieElbab 1392 days ago
I see your point. However you don’t have to define that thunk inline. You can write it separately and just pass it by name retry(…)(thunk). Not sure what signature of the retry method looks like.
1 comments

You can define it separately but it's often not the idiom. Especially for call-by-name arguments like in this case:

    def retry[A](backoffSeconds: Seq[Int])(action: => A): A
In this case you can't just define a 'val action = ...' because you don't want it to be run immediately, you want to run it inside the 'retry' method only. You could define it as a 'def' but then you're just introducing two different delayed evaluation concepts into this small piece of code. The cleanest way is to just directly pass in the action and Scala ensures it is delayed evaluation thanks to the CBN parameter type:

   retry(Seq(1, 2, 3)) {
     println("Trying")
   }