'Scala - Return value from an anonymous function

I've a named function that as an Either[A, List[B]] as return type. Inside that named function, I have a anonymous function that I'd like to make it return a value to the named function. Instead I keep getting the error "Non local returns are no longer supported; use scala.util.control.NonLocalReturns instead".

I did some research but wasn't able to find a solution.

Am I missing something? Is there any to workaround this problem?

Here's my code

def readEvents(SchedulingNode: Node): Either[Error, List[Event]] =
    val events: List[Either[Error, Event]] = SchedulingNode.child
      .filter(n => n.label == EVENT_NODE_NAME)
      .map(n => {
        val head: Head = n.attribute(HEAD_ATTRIBUTE) match {
          case None => return Left(EmptyProperty("head")) //Error occurs here
          case Some(s) => Head.from(s.toString()) match
            case Left(e: Error) => return Left(e); //Here also
            case Right(h: Head) => h
        }

        Event.from(head, 1) match {
          case Left(e: Error) => Left(e);
          case Right(ev: Event) => Right(ev)
        }

      }).toList
    //Rest of the code...


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source