title: Set a timeout for a scala method
body: <p>I am trying to set a timeout for a method. I have the following code inside a test. The return value of <code>f</code> is a special type I have defined. It returns <code>Interval(Int, Int)</code>.  </p>
  
  <pre><code>val f = evalfunc (a, b)
  </code></pre>
  
  <p>The evalfunc is using an external simplex solver. Sometimes it gets stuck here. I want to set a timeout for this. I have tried Future timeout following <a href="https://stackoverflow.com/questions/6227759/is-there-a-standard-scala-function-for-running-a-block-with-a-timeout/6228910">this</a>.   </p>
  
  <pre><code>def runWithTimeout[T](timeout: Long)(f: =&gt; T) : Option[T] = {
   Await.result(Future(f), timeout seconds).asInstanceOf[Option[T]] } 
  </code></pre>
  
  <p><code>def runWithTimeout[T](timeout: Long, default: T)(f: =&gt; T) : T = {
    runWithTimeout(timeout)(f).getOrElse(default)}
  </code> </p>
  
  <pre><code>val f = runWithTimeout(300){evalfunc(a, b)}
  </code></pre>
  
  <p>But it says, tools.Interval cannot be cast to scala.Option. How do I add a timer in this function? I need val <code>f</code> as <code>Interval(Int, Int)</code> and then I am doing some computation on <code>f</code>.</p>
  
  <p>My problem is that I have tried using the solution given in the link. In my case, I am trying to add the timeout for a method which returns <code>Interval(Int, Int)</code> and it says it requires scala.Option.</p>