0

The problem is that I'd like this function to return option value (in form of Option[None]) or Option[something], but it only returns a Unit. What am I missing?

  def intersection(another: Interval){
     var test: Option[Interval] = None
    if (this.isLaterThan(another) || another.isLaterThan(this)){
       test
    }
     else {
       val a = this.start.later(another.start)
       val b = this.end.earlier(another.end)
       test=Some(new Interval(a, b))
       test
       
     }
dbc
  • 104,963
  • 20
  • 228
  • 340
T.Simmari
  • 195
  • 1
  • 2
  • 5

2 Answers2

1

you should specify the type it return

def intersection(another: Interval): Option[Interval] = {
    if (this.isLaterThan(another) || another.isLaterThan(this)){
       None
    }
     else {
       val a = this.start.later(another.start)
       val b = this.end.earlier(another.end)
       Some(new Interval(a, b))

     }
}

try not to use var in scala I change the code a bit

Hamuel
  • 633
  • 5
  • 16
1

You are missing = before the first {. Methods defined without = always return Unit. Note that this syntax is actually deprecated precisely because this is a common error: https://issues.scala-lang.org/browse/SI-7605.

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487